Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Array slice to end of array of indeterminate size

Reply
Thread Tools

Array slice to end of array of indeterminate size

 
 
niall.macpherson@ntlworld.com
Guest
Posts: n/a
 
      05-22-2006
I have a string which I want to split into an array. I do not know how
many parts there are going to be , but I want everything except the
first 2 parts.

Here is what I have tried so far

------------------------------------------------------------------------------------------------------------------------------
use strict;
use warnings;
use Data:umper;

my $teststring = 'abc,def,ghi,jkl,mnop';

## Method 1 - OK but need to know how many elements
my @colnames = (split(/,/, $teststring))[2..4];
print Dumper \@colnames;

## Method 2 - OK but is there a more elegant way ?
(undef, undef, @colnames) = split(/,/, $teststring);
print Dumper \@colnames;

## Method 3 - Returns empty array
@colnames = (split(/,/, $teststring))[2..(-1)];
print Dumper \@colnames;

exit(0);
----------------------------------------------------------------------------------------------------------------------------

I was hoping I could use an array slice and use the range [2..-1] to
get from element 2 to the end (Method 3) but this gives me an empty
array.

Method 2 works but it led me to think that if I had a much bigger
string and I wanted from item 15 to the end I would have to do

(undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
undef, undef, undef, undef, undef, @colnames) = split(/,/,
$teststring);

which is messy.

Can anyone advise the best way ?

Thanks

 
Reply With Quote
 
 
 
 
DJ Stunks
Guest
Posts: n/a
 
      05-22-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have a string which I want to split into an array. I do not know how
> many parts there are going to be , but I want everything except the
> first 2 parts.
>
> Here is what I have tried so far
>
> ------------------------------------------------------------------------------------------------------------------------------
> use strict;
> use warnings;
> use Data:umper;
>
> my $teststring = 'abc,def,ghi,jkl,mnop';
>
> ## Method 1 - OK but need to know how many elements
> my @colnames = (split(/,/, $teststring))[2..4];
> print Dumper \@colnames;
>
> ## Method 2 - OK but is there a more elegant way ?
> (undef, undef, @colnames) = split(/,/, $teststring);
> print Dumper \@colnames;
>
> ## Method 3 - Returns empty array
> @colnames = (split(/,/, $teststring))[2..(-1)];
> print Dumper \@colnames;
>
> exit(0);
> ----------------------------------------------------------------------------------------------------------------------------
>
> I was hoping I could use an array slice and use the range [2..-1] to
> get from element 2 to the end (Method 3) but this gives me an empty
> array.
>
> Method 2 works but it led me to think that if I had a much bigger
> string and I wanted from item 15 to the end I would have to do
>
> (undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
> undef, undef, undef, undef, undef, @colnames) = split(/,/,
> $teststring);
>
> which is messy.
>
> Can anyone advise the best way ?
>
> Thanks


if you always want the same number of elements (ie: last 3) then just
use negative indexing in your slice:

my @slice = @original_array[ -3..-1 ];

if you always want from the 2nd element to the end of the array

my @slice = @original_array[ 2..$#original_array ];

-jp

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      05-22-2006

Quoth (E-Mail Removed):
> I have a string which I want to split into an array. I do not know how
> many parts there are going to be , but I want everything except the
> first 2 parts.
>
> Here is what I have tried so far
>
> ------------------------------------------------------------------------------------------------------------------------------
> use strict;
> use warnings;
> use Data:umper;
>
> my $teststring = 'abc,def,ghi,jkl,mnop';
>
> ## Method 1 - OK but need to know how many elements
> my @colnames = (split(/,/, $teststring))[2..4];
> print Dumper \@colnames;
>
> ## Method 2 - OK but is there a more elegant way ?
> (undef, undef, @colnames) = split(/,/, $teststring);
> print Dumper \@colnames;
>
> ## Method 3 - Returns empty array
> @colnames = (split(/,/, $teststring))[2..(-1)];
> print Dumper \@colnames;
>
> exit(0);
> ----------------------------------------------------------------------------------------------------------------------------
>
> I was hoping I could use an array slice and use the range [2..-1] to
> get from element 2 to the end (Method 3) but this gives me an empty
> array.
>
> Method 2 works but it led me to think that if I had a much bigger
> string and I wanted from item 15 to the end I would have to do
>
> (undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,
> undef, undef, undef, undef, undef, @colnames) = split(/,/,
> $teststring);
>
> which is messy.
>
> Can anyone advise the best way ?


my @colnames = split /,/, $teststring;
splice @colnames, 0, 2;

Ben

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
'Alcestis') [ flame, and falls out of sight. ] (E-Mail Removed)
 
Reply With Quote
 
niall.macpherson@ntlworld.com
Guest
Posts: n/a
 
      05-22-2006

DJ Stunks wrote:


>
> if you always want from the 2nd element to the end of the array
>
> my @slice = @original_array[ 2..$#original_array ];
>
> -jp


Yes - this is what I wanted , i.e from the 2nd element (or nth element
in the general case) to the end. I was aware of this method but I was
really wondering how I could do it without using an intermediate array

This works just fine
my @original_array = split(/,/, $teststring);
my @slice = @original_array[2..$#original_array];
print Dumper \@slice;

but I wanted if possible to do the assignment and the split in a single
line to make the code clearer.

If $teststring is very large I will end up making a copy each time and
since I never need the data in $teststring which comes before the third
comma this seems wasteful. It also introduces an extra variable which
does not help with readablilty.

original_array never needs to be used anywhere else in the code so I
would like to eliminate it if possible.

 
Reply With Quote
 
niall.macpherson@ntlworld.com
Guest
Posts: n/a
 
      05-22-2006
Just realised that this

@colnames = split(/,/, $teststring);
splice(@colnames, 0, 2);
print Dumper \@colnames;

is a bit better as it avoids using the temporary array but I would
still like to get it on a single line if possible.

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      05-22-2006
(E-Mail Removed) schreef:

> I have a string which I want to split into an array. I do not know how
> many parts there are going to be , but I want everything except the
> first 2 parts.



I think the "(undef, undef, @ary)" way is fine.

Just another variant:

#!/usr/bin/perl
use strict ;
use warnings ;
use Data:umper ;

my $str = 'abc,def,ghi,jkl,mnop' ;

# my (undef, undef, @colnames) = split ',', $str ;
my @colnames = do{ local @_=split',',$str; @_[2..$#_] } ;

print Data:umper->Dump( [\@colnames], [qw(*colnames)] ) ;

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      05-22-2006
(E-Mail Removed) wrote:
> Just realised that this
>
> @colnames = split(/,/, $teststring);
> splice(@colnames, 0, 2);
> print Dumper \@colnames;
>
> is a bit better as it avoids using the temporary array but I would
> still like to get it on a single line if possible.
>


OK. Here it is on one line, just for fun, but I don't think it's
clearer, let alone more efficient

----

#!/usr/bin/perl
use strict;
use warnings;

my $data = '1, 2, 3, 4, 5, 6, 7, 8';
my @slice = (split /,\s*/, $data)[2..scalar(() = $data =~ m/,\s*/g)];
print join ', ', @slice;

----

Output: 3, 4, 5, 6, 7, 8

DS
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-22-2006
(E-Mail Removed) wrote:
> Just realised that this
>
> @colnames = split(/,/, $teststring);
> splice(@colnames, 0, 2);
> print Dumper \@colnames;
>
> is a bit better as it avoids using the temporary array


yes, that is the way I would do it too. Although I too wish
[2..-1] was extra magical to just do what I mean it to.

> but I would
> still like to get it on a single line if possible.


Sure it is.

my @colnames = split(/,/, $teststring); splice(@colnames, 0, 2);


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
jdamon@gmail.com
Guest
Posts: n/a
 
      05-22-2006

(E-Mail Removed) wrote:

> Yes - this is what I wanted , i.e from the 2nd element (or nth element
> in the general case) to the end. I was aware of this method but I was
> really wondering how I could do it without using an intermediate array


If you don't mind using @_,

you can do the following...


#!/usr/bin/perl

use Data:umper;

my $teststring = 'abc,def,ghi,jkl,mnop,qrs,tuv,wx,yz';

split(",", $teststring) && print Dumper @_[2..$#_];

 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      05-22-2006
(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
>
>> Yes - this is what I wanted , i.e from the 2nd element (or nth element
>> in the general case) to the end. I was aware of this method but I was
>> really wondering how I could do it without using an intermediate array

>
> If you don't mind using @_,
>
> you can do the following...
>
>
> #!/usr/bin/perl
>
> use Data:umper;
>
> my $teststring = 'abc,def,ghi,jkl,mnop,qrs,tuv,wx,yz';
>
> split(",", $teststring) && print Dumper @_[2..$#_];
>


----

#!/usr/bin/perl
use strict;
use warnings;

my $teststring = 'abc,def,ghi,jkl,mnop,qrs,tuv,wx,yz';
split(",", $teststring) && print join ' ', @_[2..$#_];

----

Output:
Use of implicit split to @_ is deprecated at ./test.pl line 6.
ghi jkl mnop qrs tuv wx yz


DS
 
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
Displaying Checkbox in indeterminate state through code behind fil =?Utf-8?B?VmlkZHM=?= ASP .Net 0 03-02-2006 09:47 AM
jprogressbar indeterminate mode tiewknvc9 Java 4 02-23-2006 04:09 PM
Slice to end of array Ronald Fischer Perl Misc 8 12-08-2005 01:19 PM
Q: Reading indeterminate pointer -> Ref to Std? Jakob Bieling C++ 1 02-20-2005 02:41 PM
Hidden read of indeterminate memory Holger Hasselbach C Programming 19 12-24-2003 12:56 AM



Advertisments