N
niall.macpherson
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
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