Connecting Tech Pros Worldwide Forums | Help | Site Map

array slice question

kr
Guest
 
Posts: n/a
#1: Jul 19 '05
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

Jim Gibson
Guest
 
Posts: n/a
#2: Jul 19 '05

re: array slice question


In article <e03cd598.0408101014.7f0f7585@posting.google.com >, kr
<irudenko@yahoo.com> wrote:
[color=blue]
> 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().[/color]

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.
Gunnar Hjalmarsson
Guest
 
Posts: n/a
#3: Jul 19 '05

re: array slice question


Jim Gibson wrote:[color=blue]
> In article <e03cd598.0408101014.7f0f7585@posting.google.com >, kr
> <irudenko@yahoo.com> wrote:[color=green]
>>
>> 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().[/color]
>
> 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.[/color]

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
kr
Guest
 
Posts: n/a
#4: Jul 19 '05

re: array slice question


Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<iRdSc.100508$dP1.348381@newsc.telia.net>...
...[color=blue]
> 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;[/color]

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..
Closed Thread