473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 9865
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Bazell | last post by:
I have a multidimensional array, say 20 x 6, and I want to take a subset of the rows. I have an index array @indx=(1,3,12,17) for example, and what I want to say is @new_array = $data I know...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
1
by: fahd | last post by:
Hi, I'm trying to communicate with an unmanaged c++ dll that takes two 2d float arrays, one as input with data in it and the other will have the result of the operation in it as follows: bool...
7
by: RubyRedRick | last post by:
I bought Crockford's "JavaScript: The Good Parts" yesterday to help build my JavaScript foo. On page 44, he gives an implementation of the curry function: Function.method('curry', function() {...
18
by: dhtml | last post by:
Array.splice({}) What should it do? I think it should return a new Array with length 0. Array.splice(arr, start, deleteCount ]]) http://bclary.com/2004/11/07/#a-15.4.4.12 Example:
12
by: lorlarz | last post by:
In the code sample below, how are arguments a legitimate argument to Array.slice? Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.