Number of elements in an array

G

Graham

I hope the subject isn't too misleading... I am trying to find the
number of elements of each "data" array in the following:

--
use Data::Dumper;

@data = (1.0,2.0,3.0);
@test=(
{"id"=>"aaa", "units"=>"kW/m^2/st", "data"=>[@data]},
);

@data = (2.0,4.0,6.0);
push @test, {"id"=>"bbb", "units"=>"rad", "data"=>[@data]};

print Dumper @test;
print scalar($test[0]{"data"}), "\n";
--

All I can get is an address like ARRAY(0x64a0)? Surely it cannot be
that difficult to find out that there are 3 elements in each data
array ;)

Thanks.
 
R

Ron Reidy

Here is my output:

DB<3> x @test
0 HASH(0x8393598)
'data' => ARRAY(0x83882f8)
0 1
1 2
2 3
'id' => 'aaa'
'units' => 'kW/m2/st'
1 HASH(0x824bef8)
'data' => ARRAY(0x82a0a60)
0 2
1 4
2 6
'id' => 'bbb'
'units' => 'rad'

As you see, data is an array ref...so you should say 'print scalar
@{$test[0]{data}' and you will see what you are hoping to.
I hope the subject isn't too misleading... I am trying to find the
number of elements of each "data" array in the following:

--
use Data::Dumper;

@data = (1.0,2.0,3.0);
@test=(
{"id"=>"aaa", "units"=>"kW/m^2/st", "data"=>[@data]},
);

@data = (2.0,4.0,6.0);
push @test, {"id"=>"bbb", "units"=>"rad", "data"=>[@data]};

print Dumper @test;
print scalar($test[0]{"data"}), "\n";
--

All I can get is an address like ARRAY(0x64a0)? Surely it cannot be
that difficult to find out that there are 3 elements in each data
array ;)

Thanks.
 
C

Chief Squawtendrawpet

Graham said:
All I can get is an address like ARRAY(0x64a0)? Surely it cannot be
that difficult to find out that there are 3 elements in each data
array ;)

Based on your last two posts, I think you'd save yourself some grief
by spending just a few minutes brushing up on Perl references. I'd
start with 'perlreftut'.

Chief S.
 
G

Graham

Ron Reidy said:
Here is my output:

DB<3> x @test
0 HASH(0x8393598)
'data' => ARRAY(0x83882f8)
0 1
1 2
2 3
'id' => 'aaa'
'units' => 'kW/m2/st'
1 HASH(0x824bef8)
'data' => ARRAY(0x82a0a60)
0 2
1 4
2 6
'id' => 'bbb'
'units' => 'rad'

As you see, data is an array ref...so you should say 'print scalar
@{$test[0]{data}' and you will see what you are hoping to.

Many thanks '@{$test[0]{data}}' is exactly what I needed. And people
call perl a 'read-only' language ;)
 
G

Graham

Chief Squawtendrawpet said:
Based on your last two posts, I think you'd save yourself some grief
by spending just a few minutes brushing up on Perl references. I'd
start with 'perlreftut'.

Chief S.

Thanks Chief, that is exactly what I needed. What a bizarre language.
 
T

Tassilo v. Parseval

Also sprach Graham:
Ron Reidy said:
Here is my output:

DB<3> x @test
0 HASH(0x8393598)
'data' => ARRAY(0x83882f8)
0 1
1 2
2 3
'id' => 'aaa'
'units' => 'kW/m2/st'
1 HASH(0x824bef8)
'data' => ARRAY(0x82a0a60)
0 2
1 4
2 6
'id' => 'bbb'
'units' => 'rad'

As you see, data is an array ref...so you should say 'print scalar
@{$test[0]{data}' and you will see what you are hoping to.

Many thanks '@{$test[0]{data}}' is exactly what I needed. And people
call perl a 'read-only' language ;)

No. This is very straight-forward actually, but you may be carried away
by all those parens a little. If you have an array-ref in $ary_ref you
do

@array = @{ $ary_ref };

to dereference it back into an array.

But you can replace $ary_ref with a much more complicated expression. In
your case, the array-ref was in $test[0]{data} (actually, in
$test[0]->{data}; but the arrow can be left out as a shortcut). So the
above becomes

@array = @{ $test[0]{data} };

It can get as fancy as you want, since what you put behind @{ } can be
arbitrarily complicated Perl-code (thousands of lines if you want)...in
the end this code just has to evaluate to an array reference. This means
the last statement in it has to be the reference. Think of it as a
'return' from a subroutine:

@array = @{ print "Hello, world\n";
$test[0]{data}; };
print "@array\n";
__END__
Hello, world
1 2 3

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top