Syntax to retrieve $# from de-referenced array

Discussion in 'Perl Misc' started by sjcampbl@gmail.com, Nov 11, 2008.

  1. Guest

    I understand to retrieve the highest array subscript I use the
    syntax...

    $#MyArray

    However, what if the array is a reference held within a greater hash
    structure...

    %MyHash{MyHashKey} holds a reference to an array

    How do I retrieve $# on this?

    $#{MyHash{MyHashKey}} doesn't seem to work.
    , Nov 11, 2008
    #1
    1. Advertising

  2. wrote in news:d35182ae-6fb3-408f-8bae-f8ae88d2f0e5
    @s1g2000prg.googlegroups.com:

    > I understand to retrieve the highest array subscript I use the
    > syntax...
    >
    > $#MyArray
    >
    > However, what if the array is a reference held within a greater hash
    > structure...
    >
    > %MyHash{MyHashKey} holds a reference to an array
    >
    > How do I retrieve $# on this?
    >
    > $#{MyHash{MyHashKey}} doesn't seem to work.


    #!/usr/bin/perl

    use strict;
    use warnings;

    my %MyHash = ( MyHashKey => [1, 2, 3] );

    print $#{$MyHash{MyHashKey}}, "\n";

    __END__

    C:\Temp> h
    2


    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Nov 11, 2008
    #2
    1. Advertising

  3. wrote:
    >I understand to retrieve the highest array subscript I use the
    >syntax...
    >
    >$#MyArray


    Actually $MyArray[-1] maybe more perlish.

    >However, what if the array is a reference held within a greater hash
    >structure...


    You can still use ....[-1] :)

    >%MyHash{MyHashKey} holds a reference to an array
    >
    >How do I retrieve $# on this?


    See 'perldoc perlretut' and follow rule 1 in "Using references".

    jue
    Jürgen Exner, Nov 11, 2008
    #3
  4. Jürgen Exner <> wrote:
    > wrote:
    >>I understand to retrieve the highest array subscript I use the
    >>syntax...
    >>
    >>$#MyArray

    >
    > Actually $MyArray[-1] maybe more perlish.



    Except that it does something completely different. :)

    What is wanted is the subscript, not the value.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Nov 11, 2008
    #4
    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. Cris Curtis

    <embed> referenced page called 2 times

    Cris Curtis, Jul 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    466
    Cris Curtis
    Jul 25, 2003
  2. BH
    Replies:
    2
    Views:
    359
    Kevin Spencer
    Aug 5, 2003
  3. .rhavin
    Replies:
    7
    Views:
    112
    Heinrich Mislik
    Oct 8, 2004
  4. Henry Law
    Replies:
    8
    Views:
    275
    Eric J. Roode
    Nov 14, 2004
  5. feltra
    Replies:
    2
    Views:
    103
    Jürgen Exner
    Sep 25, 2010
Loading...

Share This Page