array slice question

K

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
 
J

Jim Gibson

kr said:
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.
 
G

Gunnar Hjalmarsson

Jim said:
kr said:
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;
 
K

kr

Gunnar Hjalmarsson said:
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..
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top