As chromatic said, your initial assignments don't quite work. You cannot put an array ("3","4) into a scalar $hash{"1"}; instead, you need to use references.

It sounds like you would like to do a recursive descent, expanding each index into itself as well as its values in the hash, if any. I'm not quite sure how you can do that and get exactly the string that you describe, but the following does something like what you describe.

#!/usr/bin/perl -w $hash{"0"} = ["1","2","15"]; $hash{"1"} = ["3","4"]; $hash{"2"} = ["5","6"]; $hash{"3"} = ["7","8"]; # Recursively expand a key in the hash into itself and its values. # Returns a reference to an array of keys and references to arrays. # Warning: If $hash{$n} contains $n then this expansion won't # terminate. sub expand { my ($key) = @_; my @result = ( $key ); # Keep the value. foreach my $subkey ( @{$hash{$key}} ) { # If it's a key, expand push @result, expand($subkey); # that too and append. } return \@result; } # Apply the algorithm to find the expanded "0" entry. $newhash{0} = expand(0) ; # Pretty print the result as a perl data structure. use Data::Dumper; print Dumper( $newhash{0} ); # Like the expand() routine, # but keep everything at the same depth. sub expandFlat { my ($key) = @_; my @result = ( $key ); foreach my $subkey ( @{$hash{$key}} ) { push @result, expandFlat($subkey); } return @result; } # Print out the flattened structure. print "(". join(",",expandFlat(0)) .")\n"; # Running this and compacting the Data::Dumper output a bit gives # # (1) # As a perl data structure: # $VAR1 = [ 0, [ '1', [ '3', [ '7' ], # [ '8' ] ], # [ '4' ] ], # [ '2', [ '5' ], # [ '6' ] ], # [ '15' ] ]; # # (2) # As a flat array # (0,1,3,7,8,4,2,5,6,15) # # which as I said, is pretty much your procedure # if not quite your outcome. #

In reply to Re: Re: Re: A little fun with hashes by barrachois
in thread A little fun with hashes by abitkin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.