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:
The size of an array located in an array of hashes...

 



chrislas
Novice

Jul 25, 2013, 10:21 AM

Post #1 of 9 (435 views)
The size of an array located in an array of hashes... Can't Post

I am unsure how to get the number of elements from an array located inside an array of hashes.

The array is generated here:

Code
$outer[$i]{data}=split(',',$outer[$i]{data_string});


Now I would like to know the size of that generated array, and I thought this would do the trick:

Code
$#{$outer[$i]{data}}


It results in :
Can't use string ("0") as an ARRAY ref while "strict refs" in use

What am I doing wrong, and what is the correct syntax for getting the size of that array?

Any help would be greatly appreciated!

- Christian


FishMonger
Veteran / Moderator

Jul 25, 2013, 10:30 AM

Post #2 of 9 (431 views)
Re: [chrislas] The size of an array located in an array of hashes... [In reply to] Can't Post

Your assignment is wrong.

Try

Code
@{ $outer[$i]{data} } = split(',',$outer[$i]{data_string}); 
my $count = @{ $outer[$i]{data} };



chrislas
Novice

Jul 25, 2013, 10:46 AM

Post #3 of 9 (426 views)
Re: [FishMonger] The size of an array located in an array of hashes... [In reply to] Can't Post

Thank you very much!

Quick followup-question:
The $count value for an empty array should still be -1, right? I'm seeing 0's for empty arrays, but it could be another bug in my own code..I assume the SPLIT would return an empty array there is the string it tries to split is blank?


BillKSmith
Veteran

Jul 25, 2013, 10:50 AM

Post #4 of 9 (425 views)
Re: [FishMonger] The size of an array located in an array of hashes... [In reply to] Can't Post

The following code is equivalent to FishMonger's in this case, but I find it easier to understand.

Code
$outer[$i]{data} = [split(',',$outer[$i]{data_string})]; 
my $count = @{ $outer[$i]{data} };

Good Luck,
Bill


FishMonger
Veteran / Moderator

Jul 25, 2013, 11:55 AM

Post #5 of 9 (417 views)
Re: [BillKSmith] The size of an array located in an array of hashes... [In reply to] Can't Post

However, you're now using 2 different syntax's to refer to the same thing, which IMO does not make it easier to understand.

My preference would be to be consistent in how I referred to the array. And if we want to make it easier to understand (for "newbes"), we may want to be slightly more verbose.


Code
my $count = scalar @{ $outer[$i]{data} };



(This post was edited by FishMonger on Jul 25, 2013, 11:58 AM)


FishMonger
Veteran / Moderator

Jul 25, 2013, 12:14 PM

Post #6 of 9 (413 views)
Re: [chrislas] The size of an array located in an array of hashes... [In reply to] Can't Post


Quote
The $count value for an empty array should still be -1, right?

No, it should be 0.


BillKSmith
Veteran

Jul 25, 2013, 1:04 PM

Post #7 of 9 (410 views)
Re: [FishMonger] The size of an array located in an array of hashes... [In reply to] Can't Post

FishMonger,

I see your point, but I still prefer to see the anonymous array and its reference created explicitly by the operator thats intended to do just that. I suppose that is why we have "....more than one way to do it."
Good Luck,
Bill


FishMonger
Veteran / Moderator

Jul 25, 2013, 1:13 PM

Post #8 of 9 (409 views)
Re: [BillKSmith] The size of an array located in an array of hashes... [In reply to] Can't Post


Quote
I suppose that is why we have "....more than one way to do it."


Agreed :)


chrislas
Novice

Jul 27, 2013, 6:25 AM

Post #9 of 9 (400 views)
Re: [FishMonger] The size of an array located in an array of hashes... [In reply to] Can't Post

Regardless of preferred flavor, thank you for your assistance :)

 
 


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

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