Professional Web Applications Themes

Number of elements in an array - PERL Miscellaneous

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....

  1. #1

    Default 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

  2. #2

    Default 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

  3. #3

    Default Re: Number of elements in an array

    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 Guest

  4. #4

    Default Re: Number of elements in an array

    Ron Reidy <r_reidycomcast.net> wrote in message news:<3F5FAC79.9040301comcast.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 ;)
    Graham Guest

  5. #5

    Default Re: Number of elements in an array

    Chief Squawtendrawpet <csedu.edu> wrote in message news:<3F5FB543.DD50ABDDedu.edu>...
    > 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 Guest

  6. #6

    Default Re: Number of elements in an array

    Also sprach Graham:
    > Ron Reidy <r_reidycomcast.net> wrote in message news:<3F5FAC79.9040301comcast.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 ;)
    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$&."'!#+ii ixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval Guest

  7. #7

    Default Re: Number of elements in an array

    Graham (GrahamWilsonCAyahoo.ca) wrote on MMMDCLXIII September MCMXCIII
    in <URL:news:eda30d78.0309102247.29b143c6posting.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

Similar Threads

  1. How to find the number of elements in an array
    By James Starritt in forum PHP Programming
    Replies: 5
    Last Post: January 2nd, 06:30 PM
  2. Replies: 3
    Last Post: October 6th, 03:10 PM
  3. Accessing elements in array ref of array references
    By fatted in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 24th, 08:58 PM
  4. Q- Empirical usable upper limit on hash array number of elements
    By Eric Pement in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 22nd, 06:15 PM
  5. elements 2 serial number
    By Richard Coencas in forum Adobe Photoshop & Illustrator
    Replies: 4
    Last Post: July 14th, 10:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139