Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > last index of array reference

Reply
Thread Tools

last index of array reference

 
 
Ryan Tate
Guest
Posts: n/a
 
      11-13-2003
If I have an array @array, I can get the last index with $#array.

What is the equivalent way to get the last index of array reference
$array?

I thought $#{@$array} would work, but perl coughed up "Bizarre copy of
ARRAY in leave at /script.pl line 230."

Haven't been able to find anything in this in Camel or Google Groups.

Thanks for any help

Cheers
r

 
Reply With Quote
 
 
 
 
Michael Budash
Guest
Posts: n/a
 
      11-13-2003
In article <bovdd1$1l57$(E-Mail Removed)>,
Ryan Tate <(E-Mail Removed)> wrote:

> If I have an array @array, I can get the last index with $#array.
>
> What is the equivalent way to get the last index of array reference
> $array?
>
> I thought $#{@$array} would work, but perl coughed up "Bizarre copy of
> ARRAY in leave at /script.pl line 230."


works for me using perl 5.8.1

--
Michael Budash
 
Reply With Quote
 
 
 
 
Paul van Eldijk
Guest
Posts: n/a
 
      11-13-2003
On Thu, 13 Nov 2003 07:55:13 +0000 Ryan Tate wrote:

> If I have an array @array, I can get the last index with $#array.
>
> What is the equivalent way to get the last index of array reference
> $array?
>
> I thought $#{@$array} would work, but perl coughed up "Bizarre copy of
> ARRAY in leave at /script.pl line 230."


Try: $#{$array}

HTH
Paul

--
$_=q{ ^4;c;14;1b:a^5;16:c^17:e^a;11;19:h^9;15:j^0:k^18:l ^13
:n^6^7^10:r^b;12;1a:s^2:t^3;8:u^1};s{(?<=[;^])(\d)?([\d
abc])}{$a=$1;$2=~/([abc])/?$a*13+ord($1)%87:$1*13+$2}egx;
for(split/:/){($a,@_)=split/[;^]/;@@[@_]=($a)x@_}print@@

 
Reply With Quote
 
Louis Erickson
Guest
Posts: n/a
 
      11-13-2003
Ryan Tate <(E-Mail Removed)> wrote:
: If I have an array @array, I can get the last index with $#array.

: What is the equivalent way to get the last index of array reference
: $array?

: I thought $#{@$array} would work, but perl coughed up "Bizarre copy of
: ARRAY in leave at /script.pl line 230."

: Haven't been able to find anything in this in Camel or Google Groups.

Hmm. I thought, "What a cool error message!" and wanted to see what
'use diagnostics' would say about it. So I wrote:

#! /usr/bin/perl

use strict;
use warnings;
my @a=qw(One Two Three);
my $r = \@a;
print "num: ", $#a, "\n";
print "num: ", $#{@$r}, "\n";

__END__

Running it gets:

$ perl foo.pl
num: 2
num: 2

That looks to be working just as you expected it to, doesn't it?

What version of Perl are you using? My 5.6.0 on Linux and 5.6.1 on Win32
both work. 5.8.2 for Cygwin also works. Those are all I have handy
at the moment.

--
Louis Erickson - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://www.rdwarf.com/~wwonko/

Stay away from flying saucers today.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-13-2003
Michael Budash <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In article <bovdd1$1l57$(E-Mail Removed)>,
> Ryan Tate <(E-Mail Removed)> wrote:
>
> > If I have an array @array, I can get the last index with $#array.
> >
> > What is the equivalent way to get the last index of array reference
> > $array?
> >
> > I thought $#{@$array} would work, but perl coughed up "Bizarre copy of
> > ARRAY in leave at /script.pl line 230."

>
> works for me using perl 5.8.1


It does, in 5.6.0 thru 5.9.0, but should it?

The canonical form is "$#{$arrayref}", which is derived from "$#array"
by replacing the identifier "array" with a block "{$arrayref}" which
returns a reference (symbolic or real) to an object of the right type.

Why it works with an array instead of an array reference is anybody's
guess.

Anno
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-13-2003

Louis Erickson <(E-Mail Removed)> wrote:
> Ryan Tate <(E-Mail Removed)> wrote:
> : I thought $#{@$array} would work, but perl coughed up "Bizarre copy of
> : ARRAY in leave at /script.pl line 230."
>
> Hmm. I thought, "What a cool error message!" and wanted to see what
> 'use diagnostics' would say about it. So I wrote:

<snip>

perldiag says it's an internal error (otherwise known as a bug in perl
... rather as I suspected.

Interestingly, $#@a fails as expected, but $#{@a} succeeds...

Ben

--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
(E-Mail Removed) <=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=> (Kate Rusby)
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-13-2003
Abigail <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel ((E-Mail Removed)-berlin.de) wrote on MMMDCCXXVI
> September MCMXCIII in <URL:news:bovn7e$kku$(E-Mail Removed)-Berlin.DE>:
> }} Michael Budash <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> }} > In article <bovdd1$1l57$(E-Mail Removed)>,
> }} > Ryan Tate <(E-Mail Removed)> wrote:
> }} >
> }} > > If I have an array @array, I can get the last index with $#array.
> }} > >
> }} > > What is the equivalent way to get the last index of array reference
> }} > > $array?
> }} > >
> }} > > I thought $#{@$array} would work, but perl coughed up "Bizarre copy of
> }} > > ARRAY in leave at /script.pl line 230."
> }} >
> }} > works for me using perl 5.8.1
> }}
> }} It does, in 5.6.0 thru 5.9.0, but should it?
>
> It has always worked this way, AFAIK.


Apparently. I never noticed.

[...]

> It seems to work with just $#{@array}. @{@array} doesn't give you a list
> of elements of @array.


No, it does what it should do: evaluate @array in scalar context, i.e.
take its length, and access the corresponding regex capture variable $<n>
(if symbolic references are allowed).

Similarly, $#{ @array} "ought to" return the top index of @<n>. The
actual behavior could be meant to be a case of DWIM, though I doubt it.
It isn't documented, afaik, and it shouldn't be used except for obfuscation.

Anno
 
Reply With Quote
 
Ryan Tate
Guest
Posts: n/a
 
      11-13-2003
Abigail <(E-Mail Removed)> wrote:
| It seems to work with just $#{@array}. @{@array} doesn't give you a list
| of elements of @array.

I tried a simple replication of the error with perl5.005 -e and
couldn't immediately reproduce, so I did some digging.

Strangely, the problem appears related to taint mode.

Here is how I was able to reproduce the problem. This was done under
perl5.005 and then the same behavior was reproduced under perl5.6 and
perl5.8.

First of all, it is not reproducable from the command line using -e
(!), as I will show below. So I created the file test_leave_error,
which contains (between the lines):

----------------
my $arrayref=['one','two','three'];
print foreach 0 .. $#{@$arrayref};
----------------

Then I tried the following from the shell. (I have inserted two
linebreaks to make the perl output, when there was some, stand out
from the shell prompt):

----------------
apocalypse.OCF.Berkeley.EDU [56] perl -wT test_leave_error
Bizarre copy of ARRAY in leave at test_leave_error line 2.
apocalypse.OCF.Berkeley.EDU [57] perl -w test_leave_error
012
apocalypse.OCF.Berkeley.EDU [58] perl -ewT '$arrayref=["one","two","three"];print foreach 0 .. $#{@$arrayref}'
apocalypse.OCF.Berkeley.EDU [59] perl -ew '$arrayref=["one","two","three"];print foreach 0 .. $#{@$arrayref}'
apocalypse.OCF.Berkeley.EDU [60] perl -e '$arrayref=["one","two","three"];print foreach 0 .. $#{@$arrayref}'
012
apocalypse.OCF.Berkeley.EDU [61]
----------------

As you can see, the shell produced no output at all until warnings
were turned off.

At this point, in my code, which must run under taint mode, I have
reverted to using ((length @$arrayref) - 1).

Cheers
R
 
Reply With Quote
 
Ryan Travis Tate
Guest
Posts: n/a
 
      11-13-2003
Ryan Tate <(E-Mail Removed)> wrote:
| At this point, in my code, which must run under taint mode, I have
| reverted to using ((length @$arrayref) - 1).

Erm, after re-reading the thread, I have switched to the significant
less brain dead $#{$arrayref}, which seems to work just fine, taint or
no. No point, I suppose, worrying why the other version works in some
cases but not others. IIRC & FWIW, the only reason I thought to do it
that way was I saw someone else do something like $#{1 .. 3}, which as
it turns out itself produces interesting results.
 
Reply With Quote
 
Peter Dintelmann
Guest
Posts: n/a
 
      11-14-2003
Hi,

"Ryan Tate" <(E-Mail Removed)> wrote in message
news:bovdd1$1l57$(E-Mail Removed)...
> If I have an array @array, I can get the last index with $#array.
>
> What is the equivalent way to get the last index of array reference
> $array?


perl -le '$x=[0,1,2,3]; print $#$x'
3

HTH,

Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making an array wrap, where last index + 1 = first index Shawn W_ Ruby 5 09-16-2009 02:45 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
Determine index from array reference? Knute Johnson Java 31 01-30-2007 03:58 PM
Printing array from index 1 to last element vincente13@gmail.com Perl Misc 16 05-30-2006 05:01 PM
last index of @array Martin Perl Misc 4 12-14-2003 04:38 PM



Advertisments