using a string as array indices

J

Justin

Is there an easy way to use a string as a range of array idices, ie

my @list = (0..9);
my $hi = "0..5,7,9";
print "@list[$hi]\n";

where I want the output to be:
0 1 2 3 4 5 7 9

I can think of really annoying ways to do this, but I was hoping there
would be a nice, easy way. BTW, the end use of this is so that someone
can type in a range of indices as an input (from STDIN), so if there
is an alternative way, that would be great, too.

Thanks,
Justin
 
G

Gunnar Hjalmarsson

Justin said:
Is there an easy way to use a string as a range of array idices, ie


my @list = (0..9);
my $hi = "0..5,7,9";
print "@list[$hi]\n";

where I want the output to be
0 1 2 3 4 5 7 9

I can think of really annoying ways to do this, but I was hoping
there would be a nice, easy way.

Is this annoying?

{
$, = ' ';
no warnings;
print @list[ eval $hi ], "\n";
}
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top