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:
Trying to set array equal to a returned array, but it nests it

 



perlFun
Novice

Nov 21, 2013, 5:53 AM

Post #1 of 11 (449 views)
Trying to set array equal to a returned array, but it nests it Can't Post

So - I have a hashref (call it $hashRef) with an array object that looks like this: [2,3,4] at key called $key. When I do the following

my @arr;

@arr = $hashRef->{$key};

print Dumper \@arr;

it gives me:
[ [2,3,4] ]

it's nesting it. I'm assuming it's because, when you set an array on the left equal to something on the right like a scalar, what it does is it puts that scalar in an array. But since this element on the right IS an array (it's just not obvious from the code context) - how can I make this work the way @arr1 = @arr2 would work?


Side question: how can I test if I have an actual ARRAY vs. an ARRAYREF.? All I know to do is : if( ref($myVariable) eq 'ARRAY' ) or if( ref($myVariable) eq '' ) for ex. to tell if something is a scalar v. array, but it seems that first one works for both arrays as well as arrayrefs. I'm thinking maybe the problem I have above is I actually have an arrayref and not an array, but I'm not sure how to test this out.


FishMonger
Veteran / Moderator

Nov 21, 2013, 6:55 AM

Post #2 of 11 (446 views)
Re: [perlFun] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post


Quote
I'm thinking maybe the problem I have above is I actually have an arrayref and not an array


That is correct.

You can de-reference it in the @arr assignment.


Code
@arr = @{ $hashRef->{$key} };



perlFun
Novice

Nov 21, 2013, 7:06 AM

Post #3 of 11 (444 views)
Re: [FishMonger] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post

what if tihs were an actual array though, and not an arrayref? how can I test if a variable I have is an array vs. an arrayref?


FishMonger
Veteran / Moderator

Nov 21, 2013, 7:33 AM

Post #4 of 11 (440 views)
Re: [perlFun] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post


Quote
what if this were an actual array though, and not an arrayref?

It can't be an array

Hash values are always a single element, not a list/array. So, if you need to hold multiple things under one key, then that value must be a reference to something else (i.e., an array_ref, hash_ref, code_ref, etc).


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my $hashRef = {array_ref => [1,2,3],
some_key => 'single thing'
};
print Dumper $hashRef;

foreach my $key (keys %$hashRef) {
my $ref_type = ref $hashRef->{$key};
print "$key:$ref_type\n";
}



perlFun
Novice

Nov 21, 2013, 7:48 AM

Post #5 of 11 (439 views)
Re: [FishMonger] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post

So I can't do something like this:

my @arr = (1,2,3);

my %hash = (1=>@arr);

?


FishMonger
Veteran / Moderator

Nov 21, 2013, 8:06 AM

Post #6 of 11 (434 views)
Re: [perlFun] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post

Did you try it to see what would happen?


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my @arr = (1,2,3);

my %hash = (1=>@arr);
print Dumper \%hash;


Outputs:

Code
c:\test>perlfun.pl 
$VAR1 = {
'1' => 1,
'2' => 3
};


I doubt that's what you expected/wanted.


perlFun
Novice

Nov 21, 2013, 8:18 AM

Post #7 of 11 (431 views)
Re: [FishMonger] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post

Sorry, I know the questions are basic, unfortunately I'm not somewhere I can test it out and just trying to do some thinking on it before I actually was able to try it out. Thank you very much for the responses! I'll try it once I get in to work.


perlFun
Novice

Nov 21, 2013, 9:27 AM

Post #8 of 11 (427 views)
Re: [perlFun] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post

Ok got to work. how cool, the result is very weird!

If I go:

my @arr = (2,3);

my %hash = (1=>@arr);

I get when I dump it:

{ 1=> 2,
3=> undef,}

never realized it would do this. very useful to know!

EDIT: sorry, see you had actually posted the output in your comment. Thank you for the help, I see I was really not thinking of hashes in the right way.


(This post was edited by perlFun on Nov 21, 2013, 9:28 AM)


FishMonger
Veteran / Moderator

Nov 21, 2013, 10:12 AM

Post #9 of 11 (424 views)
Re: [perlFun] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post

After seeing the results, do you understand what's happening?


perlFun
Novice

Nov 21, 2013, 11:15 AM

Post #10 of 11 (422 views)
Re: [FishMonger] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post

I understand what is happening - but I'm not certain I understand why. It's just taking the first element of the array and setting that as the value for the key I tried to set the array to. And then it's going down the rest of the list and setting key/value pairs. If you can give me some insight in to the reason why it does this, I'd be very grateful.

(I understand that you can't put a 'pure list' as a value - that makes no sense now, because by definition of a hash, a key can only be assigned a single value. That's why you CAN put an arrayREF, because that truly is a variable - it's a variable referencing an array, but still a variable. But if I do $hash{key} = @array; why doesn't it just assign the first value of @array as the value of key, and then be done with it? Why do the rest?)


(This post was edited by perlFun on Nov 21, 2013, 11:16 AM)


FishMonger
Veteran / Moderator

Nov 21, 2013, 12:50 PM

Post #11 of 11 (414 views)
Re: [perlFun] Trying to set array equal to a returned array, but it nests it [In reply to] Can't Post


Code
my @arr = (2,3); 

my %hash = (1=>@arr);


Is the same as:

Code
my %hash = (1, 2, 3, undef);


=> is known as the "fat comma" and quotes its left hand operand and the @arr array is flattened to a list.

A hash takes an EVEN numbered list (i.e., "key", "value" pairs) and associates the left hand operand (key) with the right hand operand (value).

If the list has an odd number of items/elements, depending on context, undef will be added to the end to become the final value and that is what occurred in this case.

 
 


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

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