Reg. length of anonymous array

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Reg. length of anonymous array

    Hi,

    I am a novice to perl programming. I have a reference to a hash with a hash key as a reference to an anonymous array.

    For clarity, I have something like this structure

    my $rhash = {
    $rarray => [ ],
    };

    I would like to know the length of the anonymous array containing some elements.

    Thanks,
    Balaji




    Yahoo! India Education Special: Study in the UK now.
    Balaji thoguluva Guest

  2. Similar Questions and Discussions

    1. associative array length
      Is there any way of knowing the length of an associative array? I could build a static function that does the job (iterates over the as. array and...
    2. Array length problem
      I seem to have a problem with the "array length". I can't seem to find any info as to what this refers to. Can anyone tell what this means. I am...
    3. Getting last element of anonymous array
      I am trying to get the last element of the anonymous array below. Using $# I keep getting the 1st element (or so it seems anyway). What am I doing...
    4. #24897 [Com]: array_multisort() will reindex the array but not if array length is 1
      ID: 24897 Comment by: franklin_se at hotmail dot com Reported By: chro at sokrates dot uio dot no Status: ...
    5. #24897 [Opn->Asn]: array_multisort() will reindex the array but not if array length is 1
      ID: 24897 Updated by: sniper@php.net Reported By: chro at sokrates dot uio dot no -Status: Open +Status: ...
  3. #2

    Default Re: Reg. length of anonymous array

    Balaji thoguluva wrote:
    > For clarity, I have something like this structure
    >
    > my $rhash = {
    > $rarray => [ ],
    > };
    >
    > I would like to know the length of the anonymous array containing some
    > elements.
    the usual trick use be used:

    #!/usr/bin/perl -w
    use strict;

    my $h = {array => [1,3,5,7]};

    print @{$h->{array}} . "\n";
    print $#{$h->{array}} . "\n";

    __END__

    prints:

    4
    3

    david
    --
    sub'_{print"@_ ";* \ = * __ ,\ & \}
    sub'__{print"@_ ";* \ = * ___ ,\ & \}
    sub'___{print"@_ ";* \ = * ____ ,\ & \}
    sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
    David Guest

  4. #3

    Default Re: Reg. length of anonymous array

    Balaji,
    next time when you reply please send it reply to the whole group, don't just
    sent it to myself. this gives the others a chance to help you.

    On Thursday 12 February 2004 20:05, you wrote:
    > Hi David,
    >
    > Thanks for your answer. It is working but I dont know what
    > the dot operator signifies. I would appreciate if you could explain it
    > or refer me to any documentation.
    [snip]
    > >
    > > I would like to know the length of the anonymous array containing some
    > > elements.
    >
    > the usual trick use be used:
    >
    > #!/usr/bin/perl -w
    > use strict;
    >
    > my $h = {array => [1,3,5,7]};
    >
    > print @{$h->{array}} . "\n";
    > print $#{$h->{array}} . "\n";
    >
    the dot operator does 2 things in this example:

    1. it concatenates the two operants and creates a string out of them.
    2. it forces both operants to be in scalar context.

    the second point is important, consider:

    print @{$h->{array}},"\n";
    print $#{$h->{array}},"\n";

    without the dot operator, it prints:

    1357
    3

    because @{$h->{array}} is taken in list context by the 'print' function.

    david
    --
    sub'_{print"@_ ";* \ = * __ ,\ & \}
    sub'__{print"@_ ";* \ = * ___ ,\ & \}
    sub'___{print"@_ ";* \ = * ____ ,\ & \}
    sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
    David Guest

  5. #4

    Default Re: Reg. length of anonymous array

    Hi,

    How can I rearrange an array in a specific order based on the order of a
    hash? Something like this:

    my @a = qw(Mary John Dan);
    print join "\t", @a, "\n";

    my %b = ( John => 0,
    Dan => 1,
    Mary => 2);
    print "$_ => $b{$_}\n" for (keys %b);

    print "$_-$b{$_}\t" foreach sort {$b{$a} <=> $b{$b}} keys %b;

    The final order for @a expect:

    John Dan Mary

    Thanks,

    Shiping

    Shiping Guest

Posting Permissions

  • You may not post new threads
  • You may 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