|
$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
|
|
|
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
|
|
|
Ahhh....
here is what you are looking for!
$this->[$#$this]
|
|
|
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
|
|
|
|
|
|
|
|