Advertisement

04.01.2008 at 11:10AM PDT, ID: 23286704
[x]
Attachment Details

Sorting a file using Perl

Asked by rdashokraj in Perl Programming Language, Unix Operating Systems

Hello,

I have a file like given in the snippet, please let me know how can I do numeric sort w.r.t value in the second column (in descending order)?

Thanks,
AshokStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
usr/local/NETAPP/740/611R2_sysfiles_a.tar          33710.00 KB
/usr/local/NETAPP/740/fw.rom                       245.67 KB
/usr/local/NETAPP/740/filer-mrtg.tar               2007.50 KB
/usr/local/RSA/upgrade                              3.96 KB
/usr/local//RSA/ACE_SERVER/aceserver_3.3_p09_hp.tar.gz     19024.70 KB
 
/usr/local/RSA/ACE_SERVER/aceserver_3.3_p09_ra.exe         2293.89 KB
/usr/local/RSA/ACE_SERVER/aceserver_4.1_sol5.8.tar         171.00 KB
 
/usr/local/RSA/README/readme.p09.ra.txt                    2.12 KB
/usr/local/archives/README/readme.p09.txt                  4.51 KB
 
/usr/local/RSA/ACE_CLIENT/ACE_Client-3.3.hp.tar.gz         5495.71 KB
/usr/local/RSA/ACE_CLIENT/ACE_Client-4.1.hpux.tar.gz       4127.99 KB
 
/usr/local/RSA/ACE_CLIENT/ACE_Client_4.1.aix.tar.gz         1970.00 KB
 
/usr/local/RSA/ACE_CLIENT/ACE_Client-4.1.sol.tar            2020.00 KB
[+][-]04.01.2008 at 11:24AM PDT, ID: 21256662

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.02.2008 at 12:23AM PDT, ID: 21260742

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Perl Programming Language, Unix Operating Systems
Sign Up Now!
Solution Provided By: nedfine
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.02.2008 at 01:40AM PDT, ID: 21261070

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.02.2008 at 06:20AM PDT, ID: 21262696

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
04.01.2008 at 11:24AM PDT, ID: 21256662
perl -e 'print sort{(split" ",$b)[1] <=> (split" ",$a)[1]}<>'
Assisted Solution
 
04.02.2008 at 12:23AM PDT, ID: 21260742
another way :

open IN, "sort.txt" or die "cannot open input file";
while(<IN>){
 @a = split/\s+/;
$hash{$a[0]} = $a[1];}
foreach (sort { $b <=> $a } values %hash) {
        $temp = $_;
        foreach (keys %hash) {
             print "$_ $temp\n"  if($hash{$_} == $temp);}}
close IN;


Thanks
Ned
Accepted Solution
 
04.02.2008 at 01:40AM PDT, ID: 21261070
That's an O(n²) algorithm
Better might be

open IN, "sort.txt" or die "cannot open sort.txt $!";
while(<IN>){
   push @{$hash{(split)[1]}},$_;
}
close IN;
print @{$hash{$_}}  foreach sort { $b <=> $a } keys %hash;


or
perl -le 'print /\s(.*)/ for reverse sort map{/(\S*\s+((\d*)\.?\d*).*)/&&chr(33+length $3)."$2 $1"}<>'
Assisted Solution
 
04.02.2008 at 06:20AM PDT, ID: 21262696
All your scripts are working perfectly fine. But it seems like a magic for me.  Could you please explain atleast the first syntax:  perl -e 'print sort{(split" ",$b)[1] <=> (split" ",$a)[1]}<>'

Then I observed the variables $b and $a used in all the scripts. What does it do?
And what does the [1] denotes?  Please explain.
I can understand the symbol <=> used for sorting.


 
 
ozo
04.02.2008 at 11:45AM PDT, ID: 21266116
perldoc -f sort
...
               If the subroutine's prototype is "($$)", the elements to be
               compared are passed by reference in @_, as for a normal
               subroutine.  This is slower than unprototyped subroutines,
               where the elements to be compared are passed into the
               subroutine as the package global variables $a and $b (see
               example below).  Note that in the latter case, it is usually
               counter-productive to declare $a and $b as lexicals.


perldoc perlop
...
       Equality Operators
...
       Binary "<=>" returns -1, 0, or 1 depending on whether the left argument
       is numerically less than, equal to, or greater than the right argument.
       If your platform supports NaNs (not-a-numbers) as numeric values, using
       them with "<=>" returns undef.  NaN is not "<", "==", ">", "<=" or ">="
       anything (even NaN), so those 5 return false. NaN != NaN returns true,
       as does NaN != anything else. If your platform doesn't support NaNs
       then NaN is just a string with numeric value 0.



perldoc perldata
...
           $days[28]           # the 29th element of array @days
...
       List value constructors
...
       A list value may also be subscripted like a normal array.  You must put
       the list in parentheses to avoid ambiguity.  For example:
 
 
 
20080716-EE-VQP-32 / EE_QW_2_20070628