Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Syntax to retrieve $# from de-referenced array (http://www.velocityreviews.com/forums/t908882-syntax-to-retrieve-from-de-referenced-array.html)

sjcampbl@gmail.com 11-11-2008 08:58 PM

Syntax to retrieve $# from de-referenced array
 
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.

A. Sinan Unur 11-11-2008 09:25 PM

Re: Syntax to retrieve $# from de-referenced array
 
sjcampbl@gmail.com 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 <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Jürgen Exner 11-11-2008 09:29 PM

Re: Syntax to retrieve $# from de-referenced array
 
sjcampbl@gmail.com 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

Tad J McClellan 11-11-2008 09:59 PM

Re: Syntax to retrieve $# from de-referenced array
 
Jürgen Exner <jurgenex@hotmail.com> wrote:
> sjcampbl@gmail.com 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/"


All times are GMT. The time now is 08:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.