Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Problem printing/storing hash

by zing (Beadle)
on Aug 10, 2012 at 12:07 UTC ( #986725=perlquestion: print w/ replies, xml ) Need Help??
zing has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, This is my code along with the output. I'm expecting this:- (a=>[b,d],c=>[d]) But its printing garbage addresses from memory
# Takes input in the form 'a,b|c' # How to run : perl code.pl 'a,b|c' 'c,d|e' 'a,d|e' # Outputs a NX3 for the above input data. # Outputs connections in Nx2 form use Data::Dumper; $arg=join(' ',@ARGV); @det=split //, $arg; for ($i=0; $i <=8; $i++) {$trip[$i]=$det[2*$i];} my @array; while (@trip) { push(@array, [ splice(@trip, 0, 3) ]); } print "@$_\n" for @array; for ($i=0; $i <=2; $i++) { for ($j=0; $j <=1; $j++) { $con[$i][$j]=$array[$i][$j];} } print "\n==========connections======\n"; print "from->to\n"; print " @$_\n" for @con; my %HoA; foreach (@con) { ($key, $value) = split; push @{$HoA{$key}}, $value; } # PRINTING THE HASH foreach (keys %HoA) { print "$_ => $HoA{$_}\n"; }

======OUTPUT======

xguest@localhost Downloads$ perl code.pl 'a,b|c' 'c,d|e' 'a,d|e'

a b c

c d e

a d e

==========connections======

from->to

a b

c d

a d

ARRAY(0x95a5fd4) :

ARRAY(0x95a6014) :

ARRAY(0x95a6134) :

Comment on Problem printing/storing hash
Select or Download Code
Re: Problem printing/storing hash
by roboticus (Canon) on Aug 10, 2012 at 12:39 UTC

    zing:

    It's not enough to simply include Data::Dumper, you actually have to call it. Add print Dumper(\%HoA),"\n"; to the end of your script to let it help you out.

    The slightly longer answer is this: If you want to display the structure of the data, you have to treat different data items differently. So you'd have to create a subroutine that would look at the data and handle each type differently, something like:

    sub print_thing { print join("\n", _thing_helper(shift)),"\n"; } sub _thing_helper { # return a list of strings representing the thing we're given my $thing = shift; # ok, what is it? if (ref $thing eq "ARRAY") { return "array [", # Turn each thing in the list into a string map( { _thing_helper($_) } @$thing ), "]"; } elsif (ref $thing eq "HASH") { return "hash {", # Turn each thing in the array into a string map( { "$_ =>" . _thing_helper($$thing{$_}) } keys %$thing) +, "}"; else { # A good solution would also do something with objects... return "$thing"; } }

    But after writing something like this, you'll probably only wind up with your own customer version of Data::Dumper anyway. But the point is that when you print a string or number, perl knows what you want: it prints the text representation of the value. For something that's a reference to some other thing, though, there are too many different ways to format the data for perl to pick out a good one for you. So it settles for telling you *what* the thing is, and a token that lets you tell whether it's the same item as another one you printed. There are different ways to display arrays, hashes and objects, so there are different packages to handle them.

    Notes:

    • The code is untested and probably wrong, as I composed it in the PM textbox.
    • I glossed over some things, so you should refer to the documentation for the details.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Problem printing/storing hash
by Marshall (Prior) on Aug 10, 2012 at 13:04 UTC
    I made some minor mods. I would suggest that you enable strict; (you will have to do some work to get it so that it doesn't complain).
    Update: Possible recoding below...
    #!/usr/bin/perl -w #use strict; #### I would recommend you do this!!! # Takes input in the form 'a,b|c' # How to run : perl code.pl 'a,b|c' 'c,d|e' 'a,d|e' # Outputs a NX3 for the above input data. # Outputs connections in Nx2 form use Data::Dumper; print "@ARGV\n"; $arg=join(' ',@ARGV); @det=split //, $arg; for ($i=0; $i <=8; $i++) {$trip[$i]=$det[2*$i];} my @array; while (@trip) { push(@array, [ splice(@trip, 0, 3) ]); } print "@$_\n" for @array; for ($i=0; $i <=2; $i++) { for ($j=0; $j <=1; $j++) { $con[$i][$j]=$array[$i][$j];} } print Dumper \@con; #### use the Dumper! print "\n==========connections======\n"; print "from->to\n"; print " @$_\n" for @con; my %HoA; foreach my $rowref (@con) { #each @con is a ref to row ($key, $value) = @$rowref; push @{$HoA{$key}}, $value; } # PRINTING THE HASH foreach (keys %HoA) { print "$_ => @{$HoA{$_}}\n"; #was missing a curly pair } __END__ C:\TEMP>perl hasharray.pl "a,b|c" "c,d|e" "a,d|e" a,b|c c,d|e a,d|e a b c c d e a d e $VAR1 = [ [ 'a', 'b' ], [ 'c', 'd' ], [ 'a', 'd' ] ]; ==========connections====== from->to a b c d a d c => d a => b d
    Update:

    Maybe I'm missing something but this seems to be overly complex. Maybe this is all that is needed?

    #!/usr/bin/perl -w use strict; # Takes input in the form 'a,b|c' # How to run : perl code.pl 'a,b|c' 'c,d|e' 'a,d|e' # Outputs a NX3 for the above input data. # Outputs connections in Nx2 form use Data::Dumper; print "Input args are: @ARGV\n"; my %hash; foreach my $input (@ARGV) { my($key, $value ) = ($input =~ m/\w/g)[0,1]; #not sure what role if any the |c or |e plays in this? push @{$hash{$key}}, $value; } print Dumper \%hash; __END__ C:\TEMP>perl hasharray2.pl "a,b|c" "c,d|e" "a,d|e" Input args are: a,b|c c,d|e a,d|e $VAR1 = { 'c' => [ 'd' ], 'a' => [ 'b', 'd' ] };
Re: Problem printing/storing hash
by BillKSmith (Chaplain) on Aug 10, 2012 at 14:45 UTC

    The values in @con are array references, not strings. They cannot be split.

    The values in %HoA are array references. They must be dereferenced before printing.

    # Takes input in the form 'a,b|c' # How to run : perl code.pl 'a,b|c' 'c,d|e' 'a,d|e' # Outputs a NX3 for the above input data. # Outputs connections in Nx2 form use Data::Dumper; $arg=join(' ',@ARGV); @det=split //, $arg; for ($i=0; $i <=8; $i++) {$trip[$i]=$det[2*$i];} my @array; while (@trip) { push(@array, [ splice(@trip, 0, 3) ]); } print "@$_\n" for @array; for ($i=0; $i <=2; $i++) { for ($j=0; $j <=1; $j++) { $con[$i][$j]=$array[$i][$j];} } print "\n==========connections======\n"; print "from->to\n"; print " @$_\n" for @con; my %HoA; foreach (@con) { # ($key, $value) = split; # replaced by wksmith my ($key, $value) = @$_; push @{$HoA{$key}}, $value; } # PRINTING THE HASH foreach (keys %HoA) { #print "$_ => $HoA{$_}\n"; #replaced by wksmith print "$_ => @{$HoA{$_}}\n"; }

    Note: Entries may be printed out of order.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://986725]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-08-22 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (164 votes), past polls