set a variable with a specified element of an array...and all elements that follows

G

gniagnia

Hi all,


My array @array contains AT LEAST 7 elements.
I would like to set a variable that will contain all elements of this
array beginning from the 7th elements.

Is it easily doable?

thanks in advance.
 
M

Mirco Wahab

gniagnia said:
My array @array contains AT LEAST 7 elements.
I would like to set a variable that will contain all elements of this
array beginning from the 7th elements.

Is it easily doable?

...
my @array = qw' 1 2 3 4 5 6 7 8 9 10 '; # qn{ .. } is missing :-/

my $index = 7;

# just copy elements above (non destructive)
my @over7n = @array[$index-1 .. @array-1];

# concatenate elements into string
my $over7s = join ',', @array[$index-1 .. @array-1];

# remove these elements from source array (destructive)
my @over7d = splice @array, $index-1;
...

please look up:

array slice
splice

Regards

Mirco
 
A

anno4000

gniagnia said:
Hi all,


My array @array contains AT LEAST 7 elements.
I would like to set a variable that will contain all elements of this
array beginning from the 7th elements.

Is it easily doable?

Sure, that's an array slice (see perldata).

my @tail = @array[ 6 .. $#array]

Anno
 
G

gniagnia

gniagnia said:
My array @array contains AT LEAST 7 elements.
I would like to set a variable that will contain all elements of this
array beginning from the 7th elements.
Is it easily doable?

...
my @array = qw' 1 2 3 4 5 6 7 8 9 10 '; # qn{ .. } is missing :-/

my $index = 7;

# just copy elements above (non destructive)
my @over7n = @array[$index-1 .. @array-1];

# concatenate elements into string
my $over7s = join ',', @array[$index-1 .. @array-1];

# remove these elements from source array (destructive)
my @over7d = splice @array, $index-1;
...

please look up:

array slice
splice

Regards

Mirco


It's working!
Thanks a lot for your help.
 
M

Mirco Wahab

Glenn said:
By "qn" I assume you mean a quoted list of numbers.

Is the basic list notation so unreadable?
@array = (1..10);
or
@arary = (2,4,6,8,10);

[ somwhere in germany (',' is "decimal point" all over the place) ]

use locale;
my @array = qn{ 3,1415 2,71828 0,57721 10^4 2^16 1+1 }

map print "$_,\n", @array;


3.14150
2.71828
0.57721
10000
65536
2


Regards

M.
 
D

Dr.Ruud

Mirco Wahab schreef:
# concatenate elements into string
my $over7s = join ',', @array[$index-1 .. @array-1];

ITYM:
my $over7s = join ',', @array[$index-1 .. $#array];
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top