Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Assign (key, value) to a hash w/o clobbering hash

by reasonablekeith (Deacon)
on Sep 28, 2006 at 13:30 UTC ( [id://575350]=note: print w/replies, xml ) Need Help??


in reply to Re: Assign (key, value) to a hash w/o clobbering hash
in thread Assign (key, value) to a hash w/o clobbering hash

because the OP doesn't want to clobber what's already in the hash. Assigning a list to a hash clears out the hash first, and then starts assigning the new values.

Update: argh, see mreece's reply to this post

Regardless, the map is completely redundant here, the following two are equivalent.

%hash = map { split( /X/, $_ ) } ( "fooXbar","bishXbash"); %hash = split /X/, ("fooXbar","bishXbash");
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^3: Assign (key, value) to a hash w/o clobbering hash
by mreece (Friar) on Sep 28, 2006 at 14:45 UTC
    those are not at all equivalent!
    DB<1> x %hash = map { split( /X/, $_ ) } ( "fooXbar","bishXbash"); 0 'foo' 1 'bar' 2 'bish' 3 'bash' DB<2> x %hash = split /X/, ("fooXbar","bishXbash"); 0 'bish' 1 'bash'
Re^3: Assign (key, value) to a hash w/o clobbering hash
by wazoox (Prior) on Sep 29, 2006 at 15:45 UTC
    because the OP doesn't want to clobber what's already in the hash.

    Maybe, but the OP proposed a solution that do reset the hash :

    # Works, but needs redundent array undef %hash; @array = (split /X/, "fooXbar"); $hash{$array[0]} = $array[1]; @array = (split /X/, "bishXbash"); $hash{$array[0]} = $array[1]; foreach(keys %hash){ print "2: $_ = $hash{$_}\n"; }

    so what is to be achieved isn't that clear after all.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 19:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found