3

I'm trying to get the number of "subarrays" in an array of arrays...

i.e.:

Original:

@array = (("a","b","c"),(1,2,3),("q","w","e"));

Modified:

@array = (["a","b","c"],[1,2,3],["q","w","e"]);

How would I go about getting this number? (In this case, it would be 3.)

14
  • 1
    What does print scalar(@array) give you? I think the answer is 9 because the 'sub-arrays' don't exist -- they've been flattened into one array. You'd need to use array references to get sub-arrays, with square brackets around the sub-arrays. Commented Jun 4, 2014 at 20:52
  • it outputs scalar(ARRAY(0x80872ec)) Commented Jun 4, 2014 at 20:54
  • 2
    If it says ARRAY(0xfoo) you do not have what you are saying you have in your question. Please show real code, or if it's from a hash(ref) as said in a comment on an answer, please show real debug output. Commented Jun 4, 2014 at 20:57
  • Check your typing; that (scalar(ARRAY(0x80872ec)) is nothing like what Perl 5.18.1 gives -- it gives 9. Commented Jun 4, 2014 at 20:57
  • 3
    It is hard for people to help you accurately when you don't ask the question accurately. Your change was highly non-trivial, but people had answered in good faith based on the original code. It would be best to show an SSCCE (Short, Self-Contained, Correct Example), and the exact output you get from the code you post as the SSCCE. If print scalar(@array) is not yielding 3 in the amended code, you might need to show us what use Data::Dumper; and print Dumper(\@array); prints. Commented Jun 4, 2014 at 21:04

3 Answers 3

4

you dont have any "subarrays".

my @array = (['a','b','c'],['a','b','c'],['a','b','c']);

and then just

print scalar @array;

which prints

3

you want an array of array, which can be done by having an array of arrayrefs. An Array Reference can be created by using [,] square brackets.

my $array_ref = ['a','b','c'];

You can de-reference an Arrayref with an leading @:

print scalar @$array_ref;
# or an more readable version
print scalar @{ $array_ref }
Sign up to request clarification or add additional context in comments.

4 Comments

I do have "subarrays", the way I had it originally in the question isn't how it is in my code.. sorry.
when I try printing it like that I get this output: scalar(ARRAY(0x80872ec))
@user3707618, You changed scalar(@a) to "scalar($a)"
@ikegami please look at the edits on the question :)
2

To count only array refs inside an array ref, you can use grep together with the ref built-in. Assuming there can be other things as well as array refs inside your outer array ref, you can do it like this:

my @array = (["a","b","c"],[1,2,3],["q","w","e"], 4);
my $count = scalar grep { ref $_ eq 'ARRAY' } @array;
print $count;

__END__
3

It will look at each element of @array in turn, grab the ones that have a ref type of ARRAY and force the resulting list to scalar context, giving the number of elements.


Since you seem to have an hash ref that holds array refs with array refs, maybe changing it to this will do the trick. I assumed you could do the transfer yourself.

my $count = scalar grep { ref $_ eq 'ARRAY' } @{ $hashref->{element} };

1 Comment

If there only are array refs and you want to count all of elements, the other answers are already fine.
2

Responding to modified question with info from below comment:

It sounds like what you ACTUALLY have is a hash of arrays as demonstrated in Borodin's answer to your previous question:

my %hash = (
    key => [ ["a", "b", "c"], [1, 2, 3], ["q", "w", "e"] ],
);

In which case, to get the count, you need use scalar and dereference the array ref:

print scalar(@{$hash{key}}), "\n";

Outputs:

3

4 Comments

There's not really a "multidimensional array". What you have here is a an array of arrayrefs.
the way it's defined is arbitrary, I already have the array (it's reading from a hash)
@user3707618 Since you're reading from a hash, you need to dereference the value to actually get the count.
"Same Error" communicates nothing when you've not talked about an error before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.