Number of elements in an array

Discussion in 'Perl Misc' started by Graham, Sep 10, 2003.

  1. Graham

    Graham Guest

    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, Sep 10, 2003
    #1
    1. Advertising

  2. Graham

    Ron Reidy Guest

    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, Sep 10, 2003
    #2
    1. Advertising

  3. 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.
     
    Chief Squawtendrawpet, Sep 11, 2003
    #3
  4. Graham

    Graham Guest

    Ron Reidy <> wrote in message news:<>...
    > 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 ;)
     
    Graham, Sep 11, 2003
    #4
  5. Graham

    Graham Guest

    Chief Squawtendrawpet <> wrote in message news:<>...
    > 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.


    Thanks Chief, that is exactly what I needed. What a bizarre language.
     
    Graham, Sep 11, 2003
    #5
  6. Also sprach Graham:

    > Ron Reidy <> wrote in message news:<>...
    >> 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
    --
    $_=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$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Sep 11, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. P
    Replies:
    1
    Views:
    1,174
    Joe Kesselman
    Jul 7, 2006
  2. Herrcho
    Replies:
    6
    Views:
    452
    Peter Pichler
    Feb 20, 2004
  3. Replies:
    4
    Views:
    952
    Martin Ambuhl
    May 20, 2005
  4. shyam
    Replies:
    3
    Views:
    276
    Antonio Contreras
    Feb 3, 2006
  5. Hicham Mouline
    Replies:
    1
    Views:
    400
    Kai-Uwe Bux
    Apr 11, 2010
Loading...

Share This Page