Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Initializing a hash using a foreach loop

by Roy Johnson (Monsignor)
on Nov 29, 2004 at 19:37 UTC ( [id://411023]=note: print w/replies, xml ) Need Help??


in reply to Initializing a hash using a foreach loop

You're re-assigning the whole hash every time. Try:
%ServerStatus = (); foreach my $i (0..$num) { $ServerStatus{$Ip[$i]} = [$Names[$i], 'unknown']; }
I've also removed unnecessary quotes and converted to a more Perlesque foreach loop. An alternative method would be to use map to assign the whole hash:
%ServerStatus = map {($Ip[$_], [$Names[$_], 'unknown'])} 0..$num;

Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found