Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Hash not getting updated

by CharlesClarkson (Curate)
on Apr 30, 2005 at 07:04 UTC ( #452810=note: print w/ replies, xml ) Need Help??


in reply to Hash not getting updated

I assume this is just contrived code to ask a question, but, just in case, a hash slice makes the 'foreach' block unnecessary.

my @array = ( 'white', 'blue', 'yellow' ); my %hash; @hash{ @array } = @array;

Or, without the array:

my %hash; @hash{ 'white', 'blue', 'yellow' } = ( 'white', 'blue', 'yellow' );


Comment on Re: Hash not getting updated
Select or Download Code
Re^2: Hash not getting updated
by Madam (Sexton) on May 03, 2005 at 06:34 UTC
    I had an issue in my code,that's why i had asked this question and given a small example which was very close to the issue i was facing.And your reply helped me to solve the issue. i feel instead of @hash{ @array } = @array; $hash{@array} = @array ; would be better

      Have you tried it like that?

      #!/usr/bin/perl use strict; use warnings; use Data::Dumper 'Dumper'; my @colors = qw( white yellow green ); my %hash; $hash{ @colors } = @colors; print Dumper \%hash; __END__

      I get this in perl 5.8.6:

      $VAR1 = { '3' => 3 };

      Perl assumes you want to evaluate the array in scalar context. Better to stick to the same notation everyone else uses for hash slices.

      HTH,
      Charles

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-07-11 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (224 votes), past polls