array slice question

Discussion in 'Perl' started by kr, Aug 10, 2004.

  1. kr

    kr Guest

    Hi there,

    Is it possible to slice an array from a particular element to the end
    of it without using $#<array name>? My goal is the get only the IP
    addresses from the output of gethostbyname().

    Thanks
    kr, Aug 10, 2004
    #1
    1. Advertising

  2. kr

    Jim Gibson Guest

    In article <>, kr
    <> wrote:

    > Hi there,
    >
    > Is it possible to slice an array from a particular element to the end
    > of it without using $#<array name>? My goal is the get only the IP
    > addresses from the output of gethostbyname().


    Negative indices count backwards from the end of the array, so element
    -1 is the last element in an array. Therefore, you can use
    @array[4..-1] to get an array slice from element 4 to the end.

    FYI: This newsgroup is defunct; try comp.lang.perl.misc in the future.
    Jim Gibson, Aug 11, 2004
    #2
    1. Advertising

  3. Jim Gibson wrote:
    > In article <>, kr
    > <> wrote:
    >>
    >> Is it possible to slice an array from a particular element to the
    >> end of it without using $#<array name>? My goal is the get only
    >> the IP addresses from the output of gethostbyname().

    >
    > Negative indices count backwards from the end of the array, so
    > element -1 is the last element in an array. Therefore, you can use
    > @array[4..-1] to get an array slice from element 4 to the end.


    No, he can't. The range operator does not permit that the left value
    is greater than the right value.

    The splice() function is an option, though:

    my @ret = gethostbyname 'example.com';
    my @ip = map { join '.', unpack 'C4', $_ } splice @ret, 4;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 11, 2004
    #3
  4. kr

    kr Guest

    Gunnar Hjalmarsson <> wrote in message news:<iRdSc.100508$>...
    ...
    > No, he can't. The range operator does not permit that the left value
    > is greater than the right value.
    >
    > The splice() function is an option, though:
    >
    > my @ret = gethostbyname 'example.com';
    > my @ip = map { join '.', unpack 'C4', $_ } splice @ret, 4;


    That's my problem exactly.. originally I thought to simply use
    [4..-1], but that produced no results (no warning either though)..

    Actually, if I'm to use a temp variable for this, I won't need
    splice(), e.g.

    @a = gethostbyname("www.microsoft.com");
    print join "\n", map { join ".", unpack "C4", $_ } @a[4..$#a];

    Anyway, thanks to all those who took time to look at my issue..
    kr, Aug 13, 2004
    #4
    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. Dave Bazell

    slice of multidimensional array

    Dave Bazell, Jul 23, 2003, in forum: Perl
    Replies:
    2
    Views:
    4,061
  2. Replies:
    9
    Views:
    230
    David Squire
    May 22, 2006
  3. Rick
    Replies:
    9
    Views:
    111
    Ted Zlatanov
    Jul 31, 2007
  4. Christopher Benson-Manica

    array.slice() question

    Christopher Benson-Manica, Dec 6, 2005, in forum: Javascript
    Replies:
    4
    Views:
    82
    Christopher Benson-Manica
    Dec 6, 2005
  5. RubyRedRick

    Newbyish question about Array slice method

    RubyRedRick, Jun 1, 2008, in forum: Javascript
    Replies:
    4
    Views:
    108
    Thomas 'PointedEars' Lahn
    Jun 1, 2008
Loading...

Share This Page