using a string as array indices

Discussion in 'Perl' started by Justin, Jun 3, 2004.

  1. Justin

    Justin Guest

    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
    Justin, Jun 3, 2004
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Jun 3, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. b83503104
    Replies:
    3
    Views:
    3,908
    Al Bowers
    May 21, 2004
  2. Thomas Hawtin

    64-bit array indices RFE

    Thomas Hawtin, Aug 12, 2006, in forum: Java
    Replies:
    2
    Views:
    1,314
    Patricia Shanahan
    Aug 12, 2006
  3. KLaus Blank
    Replies:
    1
    Views:
    451
  4. Randell D.
    Replies:
    9
    Views:
    124
    Douglas Crockford
    Nov 23, 2003
  5. Thomas Mlynarczyk

    array.length and string indices

    Thomas Mlynarczyk, Feb 15, 2004, in forum: Javascript
    Replies:
    7
    Views:
    93
    Thomas 'PointedEars' Lahn
    Feb 27, 2004
Loading...

Share This Page