469,293 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

array slice question

kr
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
Jul 19 '05 #1
3 9712
In article <e0**************************@posting.google.com >, kr
<ir******@yahoo.com> 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.
Jul 19 '05 #2
Jim Gibson wrote:
In article <e0**************************@posting.google.com >, kr
<ir******@yahoo.com> 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
Jul 19 '05 #3
kr
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<iR*********************@newsc.telia.net>...
...
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..
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave Bazell | last post: by
24 posts views Thread by RyanTaylor | last post: by
7 posts views Thread by RubyRedRick | last post: by
18 posts views Thread by dhtml | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.