How to get an array except the last element using slice?

P

Peng Yu

Hi,

I want to get an array without the last element. But it seems the last
line does not do what I want. I'm wondering what is the correct way to
do it.

Thanks,
Peng

#!/usr/bin/perl

use warnings;
use strict;

my @array = ('a' , 'b', 'c' , 'd');

print "@array\n";
print "@array[0,3]\n";
print "@array[0..2]\n";
print "@array[-1]\n";
print "@array[0..-1]\n"; #I want an array except the last element.
 
V

Vicky Conlan

According to said:
Hi,

I want to get an array without the last element. But it seems the last
line does not do what I want. I'm wondering what is the correct way to
do it.

print "@array[0..-1]\n"; #I want an array except the last element.

May not be the best way, but this should do what you want:
print "@array[0..$#array-1]\n"; #I want an array except the last element.
--
 
S

sln

Hi,

I want to get an array without the last element. But it seems the last
line does not do what I want. I'm wondering what is the correct way to
do it.

Thanks,
Peng

#!/usr/bin/perl

use warnings;
use strict;

my @array = ('a' , 'b', 'c' , 'd');

print "@array\n";
print "@array[0,3]\n";
print "@array[0..2]\n";
print "@array[-1]\n";
print "@array[0..-1]\n"; #I want an array except the last element.

Why do you post here? Trying to construct test's for your
students, when your such a dumb-ass regals's in fraud.
Tell me where you teach, you will be fired tommorrow.

sln
 
S

sln

According to said:
Hi,

I want to get an array without the last element. But it seems the last
line does not do what I want. I'm wondering what is the correct way to
do it.

print "@array[0..-1]\n"; #I want an array except the last element.

May not be the best way, but this should do what you want:
print "@array[0..$#array-1]\n"; #I want an array except the last element.
Don't feed the troll !!

sln
 
T

Tim Greer

Peng said:
Hi,

I want to get an array without the last element. But it seems the last
line does not do what I want. I'm wondering what is the correct way to
do it.

Thanks,
Peng

#!/usr/bin/perl

use warnings;
use strict;

my @array = ('a' , 'b', 'c' , 'd');

print "@array\n";
print "@array[0,3]\n";
print "@array[0..2]\n";
print "@array[-1]\n";
print "@array[0..-1]\n"; #I want an array except the last element.



try: @array[0..$#array-1]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top