Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Inserting data into a hash

by ghettofinger (Monk)
on Apr 15, 2005 at 16:54 UTC ( [id://448259]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

I have a really basic question that I have been milling over for a couple of hours now and I thought I had it, but I am not getting the results I wanted. I simply need to create a hash and then add data to it through multiple "while" type loops.

Here is what I have:
while ($WOW =~ m!<table id=.*?table.*?<a href=.*?><b>(.*?)</b></a></sp +an>.*?\(from (.*?)\)!sim) { $Uip = $1; $Uuser = $2; my %hash = (key => $Uip); $hash{key} .= $Uuser; print "$hash{key} \n";
... and that will print the $hash{key} pairs to the screen, but I don't think it is putting them into a hash called %hash.

I think the code above is messed up. Is there a better way to do this? How do I run through and print out the hash when I am done to validate that it is being popluated?

I appreciate everyone's help.

Many thanks,
ghettofinger

Replies are listed 'Best First'.
Re: Inserting data into a hash
by RazorbladeBidet (Friar) on Apr 15, 2005 at 17:00 UTC
    Well, you're redefining your hash every time you are in the loop, so it's local to the loop. And you're also using a string "key" to be the key. What do you want to be the key? Uip? and the value to be Uuser? If so...
    my $hash; while ($WOW =~ m!<table id=.*?table.*?<a href=.*?><b>(.*?)</b></a></sp +an>.*?\(from (.*?)\)!sim) { $Uip = $1; $Uuser = $2; $hash{$Uip} = $Uuser; ... } foreach my $key ( keys %hash ) { print "$key is Uip and $hash{$key} is Uuser\n"; }
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
      I appreciate your help.
      Thank you very much.
Re: Inserting data into a hash
by BUU (Prior) on Apr 15, 2005 at 19:51 UTC
      Yeah, I know it. I will take your advice.
      Thanks.
Re: Inserting data into a hash
by gam3 (Curate) on Apr 15, 2005 at 16:59 UTC
    print $hash{key}, " \n";
    -- gam3
    A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-28 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found