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 with subarrays?

 



wanttolearn1
New User

Mar 31, 2014, 3:06 PM

Post #1 of 7 (2983 views)
array with subarrays? Can't Post

i have file like this
a,1,1.1.1.1
a,2,1.1.2.1
a,3,1.2.1.3
b,x,1.5.2.1
b,y,1.5.6.9

i want to create array with unique value (from first column)
here i can access the array something like
@array["b"]["y"]
and i will get : "1.5.6.9"
is this possible?


BillKSmith
Veteran

Mar 31, 2014, 3:41 PM

Post #2 of 7 (2981 views)
Re: [wanttolearn1] array with subarrays? [In reply to] Can't Post

Use a hash, not an array.

Code
use strict; 
use warnings;

my %hash = (
a => {
1 => '1.1.1.1',
2 => '1.1.3.1',
3 => '1.2.1.3',
},
b => {
x => '1.5.2.1',
y => '1.5.6.9',
},
);

print $hash{b}{y};
print "\n";

Good Luck,
Bill


wanttolearn1
New User

Mar 31, 2014, 11:24 PM

Post #3 of 7 (2975 views)
Re: [BillKSmith] array with subarrays? [In reply to] Can't Post

thank
but how do i build the hash while reading line by line from file?


Laurent_R
Veteran / Moderator

Apr 1, 2014, 12:12 AM

Post #4 of 7 (2974 views)
Re: [wanttolearn1] array with subarrays? [In reply to] Can't Post

Possibly something like this:

Code
my %hash; 
while (my $line = <$FILE>) {
my ($key1, $key2, @rest) = split /\./, $line;
$hash{$key1}{$key2} = join ".", @rest;
}


You could also use the three-argument syntax of split to get only three chunks, the two keys and the value.


(This post was edited by Laurent_R on Apr 1, 2014, 12:14 AM)


wanttolearn1
New User

Apr 1, 2014, 1:30 PM

Post #5 of 7 (2964 views)
Re: [Laurent_R] array with subarrays? [In reply to] Can't Post

thanks.
how do i loop through the keys only by sort ,
and how do i take key and get it's value?
i tried

Code
foreach my $key (sort(keys %hash)) { 
print $key
}

and got
darom,arad,10darom,beer-sheva,10merkaz,ramat-gan,10merkaz,tel-aviv,10zafon,haifa,10zafon,naharia,10

instead if just the first column like in the file.


(This post was edited by wanttolearn1 on Apr 1, 2014, 1:31 PM)


Chris Charley
User

Apr 1, 2014, 5:38 PM

Post #6 of 7 (2923 views)
Re: [wanttolearn1] array with subarrays? [In reply to] Can't Post

It looks like you split on '.' instead of ',' , a small oversight of Laurent's code. He meant below


Code
my %hash;  
while (my $line = <$FILE>) {
chomp;
my ($key1, $key2, $rest) = split /,/, $line;
$hash{$key1}{$key2} = $rest;
}


In your code sample, you probably should add a newline after your print


Code
foreach my $key (sort keys %hash) {  
print $key, "\n";
}



Laurent_R
Veteran / Moderator

Apr 2, 2014, 10:12 AM

Post #7 of 7 (2677 views)
Re: [Chris Charley] array with subarrays? [In reply to] Can't Post

Yes, small error because I could no longer see the original input when I wrote the code example. I though to remember the first fields were dot-separated, not comma-separated (and the difference is hardly visible because of the lack of code tags).


(This post was edited by Laurent_R on Apr 2, 2014, 10:27 AM)

 
 


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

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