[tpm] Array slice using a variable question

Mark Fowle mfowle at navicominc.com
Fri Jul 11 13:09:39 PDT 2008


Thanks.


-----Original Message-----
From: Ilia Lobsanov [mailto:samogon at gmail.com] 
Sent: Friday, July 11, 2008 4:07 PM
To: Mark Fowle
Cc: Toronto Perl Mongers
Subject: Re: [tpm] Array slice using a variable question

Or...

my $slice = "1..3";
my @slice = eval "($slice)";
my @in = qw(a b c d e f g h);
my @out = @in[@slice];
my $p = join(" ", at out);
print "$p\n";


On 11-Jul-08, at 4:04 PM, Ilia Lobsanov wrote:

> my @slice = (1..3);
> my @in = qw(a b c d e f g h);
> my @out = @in[@slice];
> my $p = join(" ", at out);
> print "$p\n";
>
>
> ilia.
>
> On 11-Jul-08, at 3:45 PM, Mark Fowle wrote:
>
>> I want to slice an array using info passed  in a text string (in my
>> example $slice).
>> Maybe it's because its Friday, but my forehead is starting to hurt  
>> from
>> the banging.
>>
>> Any help would be appreciated.
>>
>> Thanks in advance.
>>
>> Mark
>>
>> ---- Snipit ----
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>>
>> my $slice = "1..3";
>> my @in = qw(a b c d e f g h);
>> my @out = $in[$slice];
>> my $p = join(" ", at out);
>> print "$p\n";
>>
>> ---- Output ----
>> Argument "1..3" isn't numeric in array slice at ./test.pl line 7.
>> b
>>
>>
>> ---- I wanted ----
>> b c d
>>
>>
>> _______________________________________________
>> toronto-pm mailing list
>> toronto-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/toronto-pm
>



More information about the toronto-pm mailing list