Ask a Question related to PERL Miscellaneous, Design and Development.
-
Graham #1
Number of elements in an array
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.
Graham Guest
-
How to find the number of elements in an array
Ack -- I wanted to post this to both this group and alt.php but forgot to include this group in the original posting -- sorry. Forgive me if this... -
I need help on Array object, (summing number elements)
i.e : myArray = newArray() myArray = ; sum = Number(myArray+myArray+myArray); trace (sum) //traces 15 -
Accessing elements in array ref of array references
Currently I'm comparing the first value of each of the array references (which are stored in an array reference $ref_ref) as follows: ... -
Q- Empirical usable upper limit on hash array number of elements
carltonbrown@hotmail.com (Carlton Brown) wrote in message news:<aa611a32.0307090638.23f8297f@posting.google.com>... Hope it's not too much... -
elements 2 serial number
Your serial number should be on the CD case, or if you ordered online, you should have received it at the time of purchase. The serial number is... -
Ron Reidy #2
Re: Number of elements in an array
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.
Graham wrote:> 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.
--
Ron Reidy
Oracle DBA
Ron Reidy Guest
-
Chief Squawtendrawpet #3
Re: Number of elements in an array
Graham wrote:
Based on your last two posts, I think you'd save yourself some grief> 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 ;)
by spending just a few minutes brushing up on Perl references. I'd
start with 'perlreftut'.
Chief S.
Chief Squawtendrawpet Guest
-
Graham #4
Re: Number of elements in an array
Ron Reidy <r_reidy@comcast.net> wrote in message news:<3F5FAC79.9040301@comcast.net>...
Many thanks '@{$test[0]{data}}' is exactly what I needed. And people> 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.
call perl a 'read-only' language ;)
Graham Guest
-
Graham #5
Re: Number of elements in an array
Chief Squawtendrawpet <cs@edu.edu> wrote in message news:<3F5FB543.DD50ABDD@edu.edu>...
Thanks Chief, that is exactly what I needed. What a bizarre language.> Graham wrote:>> > 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.
Graham Guest
-
Tassilo v. Parseval #6
Re: Number of elements in an array
Also sprach Graham:
No. This is very straight-forward actually, but you may be carried away> Ron Reidy <r_reidy@comcast.net> wrote in message news:<3F5FAC79.9040301@comcast.net>...>>> 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 ;)
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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
Tassilo v. Parseval Guest
-
Abigail #7
Re: Number of elements in an array
Graham (GrahamWilsonCA@yahoo.ca) wrote on MMMDCLXIII September MCMXCIII
in <URL:news:eda30d78.0309102247.29b143c6@posting.goo gle.com>:
`'
`' Thanks Chief, that is exactly what I needed. What a bizarre language.
Feel free to use a less bizarre language instead.
Abigail
--
my $qr = qr/^.+?(;).+?\1|;Just another Perl Hacker;|;.+$/;
$qr =~ s/$qr//g;
print $qr, "\n";
Abigail Guest



Reply With Quote

