in reply to Parsing through Hashes

Recursion will help you there. (Doing it iteratively instead is also possible, of course, but less natural and elegant.) Here's one solution:

#!/usr/bin/perl use strict; use warnings; my %hash = ( "Usb1" => "Usb2,Usb3", "Usb2" => "val1,val2,Usb4", "Usb3" => "val3", "Usb4" => "val4" ); say_values(\%hash, "Usb1"); say_values(\%hash, "Usb2"); sub say_values { print_values(@_); print "\n"; } sub print_values { my ($hash_ref, $key) = @_; return unless exists $hash_ref->{$key}; my @values = split ",", $hash_ref->{$key}; foreach my $index (0..$#values) { print $values[$index]; if(exists $hash_ref->{$values[$index]}) { print "["; print_values($hash_ref, $values[$index]); print "]"; } print "," unless $index == $#values; } }

This produces:

$ perl 1098967.pl Usb2[val1,val2,Usb4[val4]],Usb3[val3] val1,val2,Usb4[val4] $

Replies are listed 'Best First'.
Re^2: Parsing through Hashes
by Anonymous Monk on Aug 29, 2014 at 23:31 UTC

    Thankyou very much AppleFritter!

    I hadnt gone through recursion so far, but now it seems its a fun thing! I took a while to understand it but its great! thanks a lot again.

      You're very welcome! *tips hat* Enjoy learning Perl, and if you have further questions down the road, the Monastery'll always be here to provide help.