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:
Array to Hash

 



worldbfree
New User

Jul 9, 2013, 10:29 AM

Post #1 of 5 (202 views)
Array to Hash Can't Post

Hi everyone,

I'm new to programming but I'm trying to learn some Perl. I'm trying to take an array and make a hash out of it. Here's the array:



rn:R00412
cpd:C00042
cpd:C00122
rn:R01197
cpd:C00026
cpd:C00091
rn:R07618
cpd:C15973
cpd:C15972
rn:R00621
cpd:C00068
cpd:C00026
cpd:C05381
rn:R03316
cpd:C05381
cpd:C15972
cpd:C00068
cpd:C16254
rn:R02570
cpd:C00091
cpd:C15973
cpd:C16254
rn:R00405
cpd:C00042
cpd:C00091
rn:R00432 rn:R00727
cpd:C00042
cpd:C00091
rn:R00268
cpd:C05379
cpd:C00026
rn:R00709
cpd:C00311
cpd:C00026
rn:R01899
cpd:C00311
cpd:C05379
rn:R00352
cpd:C00036
cpd:C00024
cpd:C00158
rn:R02164
cpd:C00042
cpd:C00122
rn:R01082
cpd:C00149
cpd:C00122
rn:R01900
cpd:C00417
cpd:C00311
rn:R01325
cpd:C00158
cpd:C00417
rn:R00351
cpd:C00036
cpd:C00024
cpd:C00158
rn:R00342
cpd:C00149
cpd:C00036
rn:R00344
cpd:C00022
cpd:C00036
rn:R00014
cpd:C00068
cpd:C00022
cpd:C05125
rn:R03270
cpd:C05125
cpd:C15972
cpd:C16255
cpd:C00068
rn:R07618
cpd:C15973
cpd:C15972
rn:R02569
cpd:C00024
cpd:C15973
cpd:C16255
rn:R01196
cpd:C00024
cpd:C00022
rn:R00362
cpd:C00158
cpd:C00036
rn:R00341
cpd:C00036
cpd:C00074
rn:R00431 rn:R00726
cpd:C00036
cpd:C00074



Basically, I'm trying to make all the rn#s keys, and all the cpd#s below each rn# the corresponding value for that key. Is this even possible?



Any help is greatly appreciated!


Laurent_R
Enthusiast / Moderator

Jul 9, 2013, 10:45 AM

Post #2 of 5 (200 views)
Re: [worldbfree] Array to Hash [In reply to] Can't Post

Yes, of course it is possible. It is even quite easy.

I'll assume you know how to open the file. Then you can read from it and do something like the following (quickly written code, untested):


Code
my %hash; 
my $key;
while (<$INPUT>) {
chomp;
(undef, $key) = split /:/, $_ if /rn/;
if (/cpd/) {
my (undef, $value) = split /:/, $_ ;
$hash{$key} = $value;
}
}

Code



      
    


FishMonger
Veteran / Moderator

Jul 9, 2013, 11:32 AM

Post #3 of 5 (191 views)
Re: [Laurent_R] Array to Hash [In reply to] Can't Post

If I understand the requirements correctly, we'll need to use a HoA.

So, I'd change:

Code
$hash{$key} = $value;

To:

Code
push @{ $hash{$key} }, $value;



BillKSmith
Veteran

Jul 9, 2013, 11:35 AM

Post #4 of 5 (189 views)
Re: [worldbfree] Array to Hash [In reply to] Can't Post

A hash can only have one value for each key. The data structure that you need is called a hash of arrays. The 'value' corresponding to a key is an array reference which refers to an array containing a list of values. Refer perldoc perldsc.
Good Luck,
Bill


worldbfree
New User

Jul 9, 2013, 12:00 PM

Post #5 of 5 (184 views)
Re: [BillKSmith] Array to Hash [In reply to] Can't Post

Ah, ok I get it. I appreciate you guys helping me. I've got a lot to learn haha.

 
 


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

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