473,666 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to sort IP address using perl script

3 New Member
Hi,

Would like to learn and know how to sort the ip address using perl script?

For example:
172.27.32.200
172.19.32.100
10.1.1.60
192.20.30.133

@ipaddress = ('172.27.32.200 ','10.1.1.60',' 192.20.30.133', '172.19.32.100' );

Don't know what to do next?

thanks,
Ken
May 30 '07 #1
4 10248
miller
1,089 Recognized Expert Top Contributor
Hello Ken,

There are likely lots of different ways to accomplish this, but here is one that I think is particularly cute.

Expand|Select|Wrap|Line Numbers
  1. my @ips = qw(
  2.     172.27.32.200
  3.     172.19.32.100
  4.     10.1.1.60
  5.     192.20.30.133
  6. );
  7.  
  8. @ips = map {s/\s+//g; $_} sort map {s/(\d+)/sprintf "%3s", $1/eg; $_} @ips;
  9.  
  10. print join "\n", @ips;
  11.  
  12. # Output is:
  13. # 10.1.1.60
  14. # 172.19.32.100
  15. # 172.27.32.200
  16. # 192.20.30.133
  17.  
- Miller
May 31 '07 #2
kk5151
3 New Member
Thanks Miller. This is excellent example

Thanks
May 31 '07 #3
perlbabu
1 New Member
Hello Ken,
we can do many ways so here is to sort based on numeric

sort is by default based on strings but if you want to sort based on number in perl
syntax:sort{$a <=>$b}


Expand|Select|Wrap|Line Numbers
  1. @ipaddress = ('172.27.32.200','10.1.1.60','192.20.30.133','172. 19.32.100');
  2. @ips=sort{$a<=>$b}(@ipaddress);
  3. foreach (@ips)
  4. {
  5. print "$_\n";
  6. }
Jan 22 '13 #4
thakar86
1 New Member
HI Miller,

May I know what does
Expand|Select|Wrap|Line Numbers
  1. $1/eg
means here.

Expand|Select|Wrap|Line Numbers
  1. {s/(\d+)/sprintf "%3s", $1/eg; $_}
Mar 24 '16 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
15988
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my script. Now I want to delete a line from a file. Line being the strings I got/saved to/from array of arrays.
10
5157
by: kraj123 | last post by:
Hi Friends, I would apprciate, if anyone could help me out regarding coding tips for writing a perl script for automating a snapshot process. Snapshot is nothing but capturingn forms from different language database local servers like russia,japan..which are in csv file taken as input and comparing with present english database forms in our system and output it as definition file. this out put should be checkin to repository....this should...
4
19680
by: jram01 | last post by:
Hi Folks, I am facing problem to to execute .sql command in sql prompt using perl script. I am able to connect sql promt using " my $odbcinput = "odbcinput.txt"; my $odbcoutput = "odbcoutput.txt"; open (ODBCSQLFH, ">$odbcinput") || die "Unable to create input file ($odbcinput) for odbcsql\n";
5
5002
by: leierwu | last post by:
1. I have a folder containing several files; 2. Now these files should be added a header with the file name in the first line; How can I complish this using perl script?
1
1954
by: jawas | last post by:
Can any body help to write the general Perl script to transfer file from one window XP machine to another window XP machine and then change the file name and send some command for e.g config, 9,9 to run that file which we changed the name. I really appreciate, I some one provide any sample Perl scipt in window enviorment. Thanks Jamal
4
4282
by: jyo123 | last post by:
Hi, Is there any way to write perl script to save the popup window into specified location.Please anyone help on this. Thanks is advance.
3
2214
by: gajendra98 | last post by:
Hi I am working on windows .....IS it possible to block the remote computer python plug-ins using perl script...could some one tell me how to perform these operations.....Thanks
3
4235
by: flow3rgirlz | last post by:
Hi all, I'm really new to Perl. I'm not sure where to post my topic as it between Perl and Visio. Anyway, I want to run a Visio macro automatically using Perl Script. $vis->Run($macro_to_run); I found this method to run in Word macro. I tried to use this macro in Word and it works. But when i use in Visio macro, it doesnt run the macro. Is there any different way to run Visio macro in Perl?
1
3016
by: sree ram | last post by:
code for getting maximum rows in excel sheet using perl script
0
8440
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8355
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8638
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.