#!/usr/bin/perl use strict; use warnings; my %switches = (switch0 => [qw/switch1 switch2 switch3/], switch1 => [qw/switch4/],); my $start = 'switch0'; print_tree(\%switches,$start,0); sub print_tree{ my ($hashref,$start,$level) = @_; my $whitespaces = ' ' x ($level * 5); print $whitespaces.$start,"\n"; for my $switch(@{$hashref->{$start}}){ if(exists $hashref->{$switch}){ print_tree($hashref,$switch,$level+1); } else{ my $whitespaces = ' ' x (($level+1) * 5); print $whitespaces.$switch,"\n"; } } }