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:
How to store unknown Array into Hash?

 



gevni
Novice

Dec 19, 2012, 9:50 PM

Post #1 of 12 (4300 views)
How to store unknown Array into Hash? Can't Post

Hi
i have some arrays, and i don't get how i can store them in hash based on key. I know that i can define hash like that:

my %hash = (
humans => [ 'hamnet', 'shakespeare', 'robyn', ],
faeries => [ 'oberon', 'titania', 'puck', ],
);

But i don't get how i can store unknown array in hash.

Code
 
for($i=0; $i <$data ; $i++){
some code ......
......
.......
for($j=0;$j <= $#$data_2; $j++){
...............
...........
}
push @new_array, $data;
#I want to store this @new_array into hash based on $i as a key. I don't want to use the reference because for every iteration @new_array contain new array value. I want some thing like that
my %join_array[$i] = @new_array;
}
output should be
0->{o,1,2,3}
1->{2,3,5}
2-> {5,7,8}
some thing like that. Thanks



Laurent_R
Veteran / Moderator

Dec 20, 2012, 12:32 AM

Post #2 of 12 (4295 views)
Re: [gevni] How to store unknown Array into Hash? [In reply to] Can't Post

An array in a hash? The syntax is more like this:

@hash{'foo'} = ['bar', 'baz'];

your remark about not using a reference seems to indicate that you don't really understand what references are about.


BillKSmith
Veteran

Dec 20, 2012, 10:15 AM

Post #3 of 12 (4285 views)
Re: [Laurent_R] How to store unknown Array into Hash? [In reply to] Can't Post

Laurent is right about your lack of understanding. Please read the FAQ

Code
perldoc -f "How do I declare/create a structure"

Be sure to also read the documents that it refers to (perlref and perldsc). I also suggest reading perllol. If you still cannot understand well enough to answer your original question, post questions about the documents here.
Good Luck,
Bill


rovf
Veteran

Dec 21, 2012, 1:21 AM

Post #4 of 12 (4271 views)
Re: [gevni] How to store unknown Array into Hash? [In reply to] Can't Post


Code
my %join_array[$i] = @new_array;


It's not entirely clear to me, what you want to achieve (and what is an *unknown* array, as opposed to a *known* one), but this piece of code has several problems:

1. The expression %join_array[$i] doesn't make sense. Do you perhaps mean $join_array{$i} ?

2. You can't localize with 'my' only one element of an array or hash. You can only localize the whole array or hash.

3. In your example (using humans and faeries), you show that a hash element should have as value the *reference* of an array, which is the correct approach. However, @new_array is not a array reference, but an array. To turn an array into a reference to the array, you have to use the \ operator, i.e.


Code
\@new_array


See perldoc perlop


Laurent_R
Veteran / Moderator

Dec 21, 2012, 2:28 PM

Post #5 of 12 (4264 views)
Re: [rovf] How to store unknown Array into Hash? [In reply to] Can't Post


In Reply To
It's not entirely clear to me, what you want to achieve (and what is an *unknown* array, as opposed to a *known* one)


By "unknown", I think that the OP simply means anonymous arrays. An issue of trying to retranslate into English the translation into some other language of word anonymous.


gevni
Novice

Dec 22, 2012, 12:38 AM

Post #6 of 12 (4259 views)
Re: [Laurent_R] How to store unknown Array into Hash? [In reply to] Can't Post

Thanks all for your suggestion. Yes I was confused about using reference. Now I get the idea by reading referred perl docs and problem solved by using reference :).



Code
  
for($i=0; $i <$data ; $i++){
some code ......
......
.......
for($j=0;$j <= $#$data_2; $j++){
...............
...........
}

push @new_array, $data;
$join_array[$i] = \@data;



Laurent_R
Veteran / Moderator

Dec 22, 2012, 6:26 AM

Post #7 of 12 (4250 views)
Re: [gevni] How to store unknown Array into Hash? [In reply to] Can't Post


In Reply To

Code
for($j=0;$j <= $#$data_2; $j++){  
}



You probably mean:


Code
for($j=0;$j <= $#data_2; $j++){  
}


because "$#$data_2" is probably not doing you any good.

BTW, there are better ways to loop through the elements of an array than the "C style" loop.

If you really need an explicit interator (the $i variable) for further use:


Code
for my $i (0..$data) { #...

or possibly using the $_ implicit variable:

Code
for (0..$data) { #...


although this later construct with $_ tends to be slightly deprecated nowodays, except in very simple or idiomatic constructs such as:

Code
do_something() foreach (0..10);


And if you don't need an explicit iterator:


Code
foreach my $data2_item (@data_2) { # ...



BillKSmith
Veteran

Dec 22, 2012, 10:14 AM

Post #8 of 12 (4234 views)
Re: [gevni] How to store unknown Array into Hash? [In reply to] Can't Post

Although this is valid code (assuming that an array @data is defined), It does not answer your original question. I do not think that you will like what it does. The values of all the elements of @join_array will be exactly the same (a reference to the array @data). It does not make any difference if the values in the @data array change between the times that you make these references.

To solve your original problem, your last statement should be:

Code
$join_array[$i] = [@new_array];

The square brackets create a new anonymous array, populate it with the current values from @new_array, and return a reference to the new array. That reference is then stored in $join_array[$i].
Good Luck,
Bill


gevni
Novice

Dec 22, 2012, 10:34 AM

Post #9 of 12 (4229 views)
Re: [BillKSmith] How to store unknown Array into Hash? [In reply to] Can't Post

What i did to solve my problem I add @new_array = (); at first loop .But your way $join_array[$i] = [@new_array]; is more sensible.


gevni
Novice

Dec 22, 2012, 10:57 AM

Post #10 of 12 (4226 views)
Re: [gevni] How to store unknown Array into Hash? [In reply to] Can't Post

Now i am trying to store the $join_array[$i] array into @array_index by using $tmp array value as an index.
For example
$join_array[0]= {AA,BB,CC,DD}
and @$tmp{2,1,0,3}# It act as index No.
Then @array_index= {CC,BB,AA,DD}

Code
 
$join_array[$i] = [@new_array];
$tmp = $per[$j]->combination($perm->[$j]);
............
..................
#I write this code for doing that but I am not sure about [@$tmp[$k]] statement, am i doing right here?

for my $k(0..$tmp)
{
push my @array_index, $join_array->[$j][@$tmp[$k]];
}



BillKSmith
Veteran

Dec 23, 2012, 12:12 PM

Post #11 of 12 (4171 views)
Re: [gevni] How to store unknown Array into Hash? [In reply to] Can't Post

No loop is needed, use an array slice.

Code
use strict; 
use warnings;
my @join_array = ([qw(AA BB CC DD)]);
my @tmp = (2, 1, 0, 3);
my @array_index = @{$join_array[0]}[@tmp];
print "@array_index\n";


OUTPUT

Code
CC BB AA DD


Edit: Added output
Good Luck,
Bill

(This post was edited by BillKSmith on Dec 24, 2012, 7:25 AM)


gevni
Novice

Dec 24, 2012, 9:13 AM

Post #12 of 12 (4105 views)
Re: [BillKSmith] How to store unknown Array into Hash? [In reply to] Can't Post

Thanks :)

 
 


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

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