Hello Monks
Thanks a million for taking a look at my code ....

I have a file in the form
id1 val1
id1 val2
id2 val1
id2 val3
id3 val2
id3 val3
id3 val4

I'm forming a HoA that associates the ids which are unique with their values that are not. I want a user to enter an id (or ids). The ids are then looked up in the hash and the array of values associated with each id is printed.

Okay so my code runs - it does hang but thats due to the silly size of the file. I get a very long list of
....
Q9ZZZ4 ARRAY(0xf4d641c)
Q9ZZZ5 ARRAY(0xf4d6464)
....
which is okay... I think. So I create my hash, I enter an id <STDIN> and the id is found or not depending on if its in the hash. So far so good. The problem is with printing the array of associated vals. I have tried a couple of methods.

The first method below but commented out - pints the first val only. print "$user: @{ $p2i{$user} }\n";

The second method prints the ref to the array .i.e. ARRAY(0xf4d4a88)

print "$user is in the hash. \n"; @user_array = split(/ /, $p2i{$user}); foreach (@user_array) { print "$_\n"; }
The full code is below. I think my problem is that I'm not dereferencing the array correctly....

#!/usr/bin/perl use strict; use warnings; my $file = "p2iextact.txt"; my %p2i; my @user_array; my $sp; my $ipr; my $key; my $user; # creates hash of arrays open(FILE, "p2iextact.txt") || die "can't open file"; while(<FILE>) { chomp; ($sp, $ipr) = split; if (exists $p2i{$sp}) { push @{$p2i{sp}}, $ipr; } else { $p2i{$sp}= [$ipr]; } } close FILE; # test to see if hash created correctly foreach $key (sort keys %p2i) { print "$key\t$p2i{$key}\n"; } print "Please enter an ID: "; chomp( $user = <STDIN>); # try to used uppercaes function to avoid errors $user = uc($user); #see if id is in the hash and print @vals if (exists $p2i{$user}) { print "$user is in the hash. \n";if (exists $p2i{$user}) { print "$user is in the hash. \n"; @user_array = split(/ /, $p2i{$user}); foreach (@user_array) { print "$_\n"; } # print "$user: @{ $p2i{$user} }\n"; # only retieves one value even when more than one #val even +if there are multiple } else { print "$user is not in the hash.\n"; } exit;

Cheers
Stalky

In reply to de-ref an array from HoA by stalkeyefly

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.