perlpreben has asked for the wisdom of the Perl Monks concerning the following question:

Ok, sorry for the second post.. but i sat the first one to "solved", so i recon im better of with a new post **sorry**.
Still struggeling with perl hashes, and here is the data im trying to extract into a variable i can work with:

$HASH1 = { dest => { 3656040223 => bless( { _frame => "\0P\347\211\331\352\277\37I|\$D\200 +\20\0.\0{\0\0\1\1\b\n,\276\217\6\3\250\210\321", _parent => undef, acknum => 1232872516, cksum => 123, data => '', dest_port => 59273, flags => 16, hlen => 8, options => "\1\1\b\n,\276\217\6\3\250\210\321", reserved => 0, seqnum => 3656040223, src_port => 80, urg => 0, winsize => 46 }, 'NetPacket::TCP' ) }, src => {} };
This data is from a variable called $test. I asked the monks before today about an example on how to do this, and that example worked. but when i try the same here, i cant get it to work....

Ive tried

print $test->dest->flags
print $test->dest->[0]->dest_port

And a lot of other variations.

I dont know if i need to use this: dest => { 3656040223 when i extract the data. And that number changes, so I cant do:

$conn->dest->3656040223->flags

Please help, and be kind on the response :) This is a field im struggeling to get the hang of.

Replies are listed 'Best First'.
Re: Perl hashes revisit
by bart (Canon) on Feb 14, 2009 at 19:25 UTC
    print $test->dest->flags
    Replace this with
    print $test->{dest}->{flags}
    or
    print $test->{dest}{flags}
    In Perl, the syntax you used is only for method calls, not for hash items. I know that in a lot of other languages the difference between the two isn't so large...
      i was unable to get any data doing it in that way... And Ive tried to read the documentation as well to better understand this.. but im still having problems. And its getting the better of me!
        Is your variable $test or %test? The code is for $test (a scalar variable, containing a hashref). If it is %test (a hash variable), change the code to
        print $test{dest}->{flags}
        or
        print $test{dest}{flags}
Re: Perl hashes revisit
by toolic (Bishop) on Feb 14, 2009 at 19:49 UTC
      OK and update... this worked:
      for my $key (keys %{$conn->tcp_connection->window->{dest}}){ my $cb = ${$conn->tcp_connection->window->{dest}}{$key}; print $cb->{src_port} . "\n\n\n"; }
      The reason is that it as far as i can tell, loops through .. and enumerates the name of the value after dest. This is the first in the loop above. But is the a way i can say .... use first instance ... insted of looping through it ???
Re: Perl hashes revisit
by Bloodnok (Vicar) on Feb 14, 2009 at 20:25 UTC
    It appears as though $test->{dest} is a hash of NetPacket::TCP objects keyed on some id that, as you've noticed, varies, so, given that $test->{dest}->{num} is a hash ref., in order to see the flags for all dest's, try...
    foreach my $num (keys %{ $test->{dest} } ) { print "$num: " . $test->{dest}->{$num}->flags(); }

    A user level that continues to overstate my experience :-))