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: Beginner:
Hashes %

 



Zoob
Novice

Jul 1, 2009, 8:23 AM

Post #1 of 8 (853 views)
Hashes % Can't Post

Hello everybody once again another problem (i am learning from my mistakes :D)

This time i need to create an array of hashes which i have sucsesfully done. But i just have the problem of printing my hashes could you all please help me. I have google searched it but, no decent result

What i have tried is

print %names; (it fails to do so)

Thanks

Zoob


marksman
Novice

Jul 1, 2009, 11:36 AM

Post #2 of 8 (848 views)
Re: [Zoob] Hashes % [In reply to] Can't Post

 
try
my @arr = %hash;
print "@arr";

edit: sorry didn't realize that link was to pirated material, I just found it via a google search


(This post was edited by marksman on Jul 2, 2009, 10:18 AM)


KevinR
Veteran


Jul 1, 2009, 2:37 PM

Post #3 of 8 (843 views)
Re: [marksman] Hashes % [In reply to] Can't Post

marksman,

please edit your post and remove the URL to the pirated resource material.

Thank you.
-------------------------------------------------


Zoob
Novice

Jul 2, 2009, 2:10 AM

Post #4 of 8 (838 views)
Re: [marksman] Hashes % [In reply to] Can't Post


In Reply To
http://docstore.mik.ua/orelly/perl/cookbook/ch05_06.htm

try
my @arr = %hash;
print "@arr";

That does not work. leme show you what i have made

%names = (
Bora => [ "Male", "15" ],
Trevor => [ "Male", "27" ],
Andy => [ "Male", "35" ],
Alex => [ "Male", "30" ],
Louise => [ "Female", "22" ],
Maria => [ "Female", "34" ],
SanJeev => [ "Male", "33" ],
Nevin => [ "Female", "43"],
Qwerty => [ "Male", "100" ] ,
);


This is the hash that i need to print to the screen can anyone help me how i will be ding this.


vikas.deep
User

Jul 2, 2009, 2:31 AM

Post #5 of 8 (836 views)
Re: [Zoob] Hashes % [In reply to] Can't Post

Dear brother
You have built a hash of arrays true but remember those arrays are anonymous and you can't pull out something from an anonymous array , the way you are trying.
Please try the following

Code
#! usr/bin/perl 
use strict;
use warnings;
my %names = (
Bora => [ "Male", "15" ],
Trevor => [ "Male", "27" ],
Andy => [ "Male", "35" ],
Alex => [ "Male", "30" ],
Louise => [ "Female", "22" ],
Maria => [ "Female", "34" ],
SanJeev => [ "Male", "33" ],
Nevin => [ "Female", "43"],
Qwerty => [ "Male", "100" ] ,
);
print $names{'Bora'}->[0]
# The output is
#[root@localhost Documents]# perl hell.pl
#Male

You see you will have to use print $names{'Bora'}->[1] to get

#perl hell.pl
#15[root@localhost Documents]#

Do it like this way for each (anonymous) array pointed to by each key of the hash

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


vikas.deep
User

Jul 2, 2009, 2:39 AM

Post #6 of 8 (835 views)
Re: [Zoob] Hashes % [In reply to] Can't Post


Quote
my @arr = %hash;
print "@arr";

should have given you something like

Quote
perl hell.pl
Nevin ARRAY(0x9cd1614) Louise ARRAY(0x9cd16a4) Bora ARRAY(0x9c94c28) SanJeev ARRAY(0x9cd1644) Qwerty ARRAY(0x9cd15e4) Alex ARRAY(0x9cd16d4) Trevor ARRAY(0x9cd1734) Andy ARRAY(0x9cd1704) Maria ARRAY(0x9cd1674)

You see here each key of your hash is pointing to an anonymous array ; if you want to use this code you have to explicitly de-refer your self Perl never automatically derefers for you. Use the following

Code
print $arr[1][0]

and you get

Code
perl hell.pl 
Female[root@localhost Documents]#



Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


KevinR
Veteran


Jul 2, 2009, 10:45 AM

Post #7 of 8 (821 views)
Re: [marksman] Hashes % [In reply to] Can't Post


In Reply To
try
my @arr = %hash;
print "@arr";

edit: sorry didn't realize that link was to pirated material, I just found it via a google search


I believe you that you did not know it was to a pirate website. Thank you for removing the link.

Regards,
Kevin
-------------------------------------------------


KevinR
Veteran


Jul 2, 2009, 10:46 AM

Post #8 of 8 (820 views)
Re: [Zoob] Hashes % [In reply to] Can't Post


In Reply To

In Reply To
**** remove this URL ****

try
my @arr = %hash;
print "@arr";

That does not work. leme show you what i have made

%names = (
Bora => [ "Male", "15" ],
Trevor => [ "Male", "27" ],
Andy => [ "Male", "35" ],
Alex => [ "Male", "30" ],
Louise => [ "Female", "22" ],
Maria => [ "Female", "34" ],
SanJeev => [ "Male", "33" ],
Nevin => [ "Female", "43"],
Qwerty => [ "Male", "100" ] ,
);


This is the hash that i need to print to the screen can anyone help me how i will be ding this.


Zoob,

please edit your post and remove the URL to the pirated resource material.

Thank you
-------------------------------------------------

 
 


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

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