Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Hash not getting updated

by Madam (Sexton)
on Apr 29, 2005 at 08:15 UTC ( #452543=perlquestion: print w/replies, xml ) Need Help??
Madam has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks, In a 'foreach' loop ,i am appending the hash.but it is taking the last values,the intial elements are not getting added. i.e,
my @array = ("white","blue","yellow"); my %hash = (); foreach my $temp (@array) { %hash = ($temp => $temp); } print keys %hash;
only "yellow" is getting printed. where am i going wrong? Thanks, Madam

Replies are listed 'Best First'.
Re: Hash not getting updated
by blazar (Canon) on Apr 29, 2005 at 08:21 UTC
    %hash = ($temp => $temp);
Re: Hash not getting updated
by davidj (Priest) on Apr 29, 2005 at 08:30 UTC
    blazar is right. That is the way you need to do it. The reason %hash had only one element in your original loop is because with each iteration of the loop it was being recreated: %hash = ($temp => $temp) .

Re: Hash not getting updated
by monkfan (Curate) on Apr 29, 2005 at 13:52 UTC

    Is this what you want?
    #!/usr/bin/perl -w use strict; use Data::Dumper; my @array = ("white","blue","yellow"); my %hash = (); foreach my $temp (@array) { $hash{$temp} = $temp; } print Dumper \%hash; print join (" ",keys %hash);
    That gives:
    $VAR1 = { 'white' => 'white', 'blue' => 'blue', 'yellow' => 'yellow' }; white blue yellow
    If you want to learn more about hash or list, check this out,it gives solid rudimentary concept.
    When I first learn Perl, I also found this information about constructing data structure in Perl very useful.

Re: Hash not getting updated
by CharlesClarkson (Curate) on Apr 30, 2005 at 07:04 UTC

    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' );
      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.

Re: Hash not getting updated
by gube (Parson) on Apr 30, 2005 at 01:22 UTC

    Hi, try this here is the problem and solution,

    The problem is your key and value is assigned to hash. Each time they assigned. so, in that final value only stored in hash and the key printed for that hash is yellow. ie. That is the problem.

    So, don't assigned to hash each time, please use like below mentioned code.

    my @array = ("white","blue","yellow"); my %hash = (); foreach my $temp (@array) { $hash{$temp}=$temp; } print keys %hash;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://452543]
Approved by blazar
Front-paged by monkfan
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-11-15 21:24 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (192 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!