#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Smile Learning Perl : Anonymous Hashes


    Hi I am trying to experiment with anonymous Hashes. I am trying to insert new values in to the hash using the push function. but i get the following error at the push function saying:
    "Not an ARRAY reference at anonymous_hash2.pl line 26."

    Below is the code i am trying to execute: Please advise
    #!/bin/perl
    # Program to demonstrate a hash containing anonymous hashes.
    my $hashref = {
    Math => { # key
    "Anna" => 100,
    "Hao" => 95, # values
    "Rita" => 85,
    },
    Science => { # key
    "Sam" => 78,
    "Lou" => 100, # values
    "Vijay" => 98,
    },
    };

    print "Anna got $hashref->{'Math'}->{'Anna'} on the Math test.\n";

    $hashref->{'Science'}->{'Lou'}=90;

    print "Lou's grade was changed to $hashref->{'Science'}->{'Lou'}.\n";

    print "The nested hash of Math students and grades is: ";

    print %{$hashref->{'Math'}}, "\n"; # Prints the nested hash, Math

    push ( @{$hashref}, {'Math'=>{"Carol"=>40 , "Aria"=>90}} );

    foreach $key (keys %{$hashref}){
    print "Outer key: $key \n";

    while(($nkey,$nvalue)=each(%{$hashref->{$key}})){
    printf "\tInner key: %-5s -- Value: %-8s\n",$nkey,$nvalue;
    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    The push function is an operator to add a new item into an array, not into a hash. Same thing with unshift.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0
    Thanks Laurent_R,

    So the only way to add elements into a hash is by assignment?

    Originally Posted by Laurent_R
    The push function is an operator to add a new item into an array, not into a hash. Same thing with unshift.
  6. #4
  7. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,251
    Rep Power
    1810
    Originally Posted by csvraju1985
    So the only way to add elements into a hash is by assignment?
    A hash key requires an assignment. The value could have been an array reference, and you could push into that.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my $hashref = {
    	'Math' => {
    		'Anna' => 100,
    		'Hao' => 95,
    		'Rita' => 85,
    	},
    	'Science' => {
    		'Sam' => 78,
    		'Lou' => 100,
    		'Vijay' => 98,
    	},
    };
    
    print Dumper $hashref;
    
    $hashref->{'Math'}{'Carol'} = 40;
    $hashref->{'Math'}{'Aria'} = 90;
    With the structure given, there can only be one student with a given first name, and they can only have one score per subject. It's not a bad structure, and makes sense in some environments.

    You can make multiple assignments at the same time though:

    Code:
    @{$hashref->{'Math'}}{('Charles','Dougie','Leroy')} = (89, 66, 78);
    
    print Dumper $hashref;

IMN logo majestic logo threadwatch logo seochat tools logo