Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > set a variable with a specified element of an array...and all elements that follows

Reply
Thread Tools

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

 
 
gniagnia
Guest
Posts: n/a
 
      02-16-2007
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.

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      02-16-2007
gniagnia wrote:
> 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
 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      02-16-2007
gniagnia <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
gniagnia
Guest
Posts: n/a
 
      02-16-2007
On 16 fév, 09:41, Mirco Wahab <(E-Mail Removed)> wrote:
> gniagnia wrote:
> > 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.

 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      02-16-2007
Glenn Jackman wrote:
> At 2007-02-16 03:41AM, "Mirco Wahab" wrote:
>> my @array = qw' 1 2 3 4 5 6 7 8 9 10 '; # qn{ .. } is missing :-/

>
> 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.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      02-16-2007
Mirco Wahab schreef:

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


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

--
Affijn, Ruud

"Gewoon is een tijger."
 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 1 01-30-2004 04:09 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 0 01-28-2004 02:50 PM
Anchor element follows HREF even though onClick returns false Johnny Javascript 4 01-19-2004 02:25 PM



Advertisments