Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > using a string as array indices

Reply
Thread Tools

using a string as array indices

 
 
Justin
Guest
Posts: n/a
 
      06-02-2004
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-02-2004
Justin wrote:
> 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";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
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
Using an array value as indices for an array KLaus Blank VHDL 1 07-07-2008 07:38 PM
64-bit array indices RFE Thomas Hawtin Java 2 08-12-2006 04:23 AM
how to efficiently do sorting and get array of indices? b83503104 C Programming 3 05-21-2004 10:44 AM
array.length and string indices Thomas Mlynarczyk Javascript 7 02-27-2004 05:43 AM
using for to read an array with string indices Randell D. Javascript 9 11-23-2003 05:24 PM



Advertisments