CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Difference between the Hash retriving method via reference and Non reference

 



C_PRASANNA
Novice

Feb 24, 2014, 1:46 AM

Post #1 of 4 (1316 views)
Difference between the Hash retriving method via reference and Non reference Can't Post

Guys,

Please explain whats the difference in the below cases on retriving the value from hash by using reference(arrow symbol).

Case 1:
my %months = (jan => 1, feb => 2, mar => 3, apr = 4, may => 5);

foreach my $mon (keys sort %months )
{
#Note the below format
$Value = $months->{$mon};
print "Value = $Value\n";
}


Case 2:
my %months = (jan => 1, feb => 2, mar => 3, apr = 4, may => 5);

foreach my $mon (keys sort %months )
{
#Note the below format
$Value = $months{$mon};
print "Value = $Value\n";
}



I mentioned a note inside the foreach loop on both above cases. Explain whats special feature in using the arrow, if not any issue. Pl let me know.

Thank You,

Regards - Prasanna.


Chris Charley
User

Feb 24, 2014, 11:48 AM

Post #2 of 4 (1289 views)
Re: [C_PRASANNA] Difference between the Hash retriving method via reference and Non reference [In reply to] Can't Post

To see the difference, look at the code below. $href is a reference to a hash (whereas %months is a hash).

Code
#!/usr/bin/perl 
use strict;
use warnings;

my %months = (jan => 1, feb => 2, mar => 3, apr => 4, may => 5);
my $href = {one => 1, two => 2, three => 3, four => 4, five => 5};

foreach my $key (sort {$href->{$a} <=> $href->{$b}} keys %$href )
{
# hash reference
my $Value = $href->{$key};
print "key = $key Value = $Value\n";
}

print "\n";

foreach my $mon (sort {$months{$a} <=> $months{$b}} keys %months )
{
# not a reference
my $Value = $months{$mon};
print "month = $mon Value = $Value\n";
}


Prints

Code
key = one Value = 1 
key = two Value = 2
key = three Value = 3
key = four Value = 4
key = five Value = 5

month = jan Value = 1
month = feb Value = 2
month = mar Value = 3
month = apr Value = 4
month = may Value = 5



Laurent_R
Veteran / Moderator

Feb 24, 2014, 2:27 PM

Post #3 of 4 (1269 views)
Re: [C_PRASANNA] Difference between the Hash retriving method via reference and Non reference [In reply to] Can't Post

If you try to run your case 1 code:


Code
my %months = (jan => 1, feb => 2, mar => 3, apr => 4, may => 5); 

foreach my $mon (keys sort %months )
{
#Note the below format
$Value = $months->{$mon};
print "Value = $Value\n";
}


it will simply not compile and give you an error message with something like this:


Code
Type of argument to keys on reference must be unblessed hashref or arrayref...


which basically means that the "arrow" notation can be used only with hashref or arrayref, not with plain hashes or plain arrays.

(Another error is that you probably want 'sort keys", not "keys sort" in the foreach line. Not sure either that you really want to sort on the keys, you more probably want to sort on the values, but that's a different issue, it may be your choice to sort the months in alphabetical order if so you wish.)

But if you create a hashref instead of a hash, then "arrow" notation will be useful:


Code
my $month_ref = {jan => 1, feb => 2, mar => 3, apr => 4, may => 5}; 
foreach my $mon (sort keys %{$month_ref} )
{
#Note the below format
$Value = $month_ref->{$mon};
print "Value = $Value\n";
}


although this:

Code
$Value = $$month_ref{$mon};

will work just as well.


C_PRASANNA
Novice

Feb 27, 2014, 11:00 AM

Post #4 of 4 (1192 views)
Re: [Laurent_R] Difference between the Hash retriving method via reference and Non reference [In reply to] Can't Post

Well Explained ... Thank You both

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives