codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  array names  adamdoubts at 01:59 on Tuesday, April 25, 2006
 

$array[$#array] gets the last element of an array.

for example the last element of @this is $this[$#this].

now here's the question. I regularly use variables to name arrays. thus I would have @$this array where $this is assigned each time through a loop. But when I try to get the last element as $$this[$#$this], perl won't let me.

I'm self taught, so I do hope this is not a stupid question. Can anybody say why this doesn't work?

Best

Adam

  Re: array names  S_Flex at 11:49 on Tuesday, April 25, 2006
 

as fare as I know this is one way an array works

my $value_2 = 555;

@array = ('value_0', "value_1", $value_2, 'est...');

@array[0] is value_0

@array[1] is value_1

@array[2] is 555

@array[3] is est...

@array[2,3] is 555 and est...

array uses numbers for the veriables place in the array.
I am lacking in array skills but a way to get the last veriable in the array you can count the number of veriables in the array then call the highest number in the array like @array[3] or try

my @item;
foreach (@array) {
@item = split (/\|/, $_);
}

my $last = @item;

That may work....

Here is a nice link that can help you too...
http://www.unix.org.ua/orelly/perl/learn/index.htm

  Re: array names  S_Flex at 15:26 on Friday, April 28, 2006
 

Ahhh....

here is what you are looking for!

$this->[$#$this]

  Re: array names  adamdoubts at 16:31 on Friday, April 28, 2006
 

yup. thank you.

for anyone watching, here's the code that proves it.

input:
d1
d2
d3
a2
a4
a6
a8

code:
$_ =~ m/(\w)(\d)/g;
$letters{$1}++;
push(@$1, $2);

foreach (keys %letters)
{
print "I have $letters{$_} of $_ and the last of them is $_->[$#$_]\n";
}

Adam








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Perl Script Output (w3c validator)
•  Re: HashMap question
•  focus management?
•  Looping Issue...Please help!
•  regarding hibernate
•  working with plugins
•  Checkbox Validation help.
•  How to Disable Submit Button -is not for multiple submit/click
•  Re: Can`t run my application


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006