LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2010, 06:58 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
Perl: sort and get total


I have this script that prints a list of server names in STDOUT. I would like to sort that in alphabetical order and get the total number of servers.
Thanks.

Code:
#!/usr/bin/env perl

use strict;
use warnings;

use Frontier::Client;

my $HOST = 'xxxxxxxxx';
my $user = 'xxxxxx';
my $pass = 'xxxxxx';

my $client = new Frontier::Client(url => "http://$HOST/rpc/api");
my $session = $client->call('auth.login',$user, $pass);

my $systems = $client->call('system.listSystems', $session);
foreach my $system (@$systems) {
  print $system->{'name'}."\n";
}
$client->call('auth.logout', $session);
 
Old 05-21-2010, 07:09 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by noir911 View Post
I have this script that prints a list of server names in STDOUT. I would like to sort that in alphabetical order and get the total number of servers.
Thanks.

Code:
#!/usr/bin/env perl

use strict;
use warnings;

use Frontier::Client;

my $HOST = 'xxxxxxxxx';
my $user = 'xxxxxx';
my $pass = 'xxxxxx';

my $client = new Frontier::Client(url => "http://$HOST/rpc/api");
my $session = $client->call('auth.login',$user, $pass);

my $systems = $client->call('system.listSystems', $session);
foreach my $system (@$systems) {
  print $system->{'name'}."\n";
}
$client->call('auth.logout', $session);
First read

Code:
perldoc -f sort
and then, if something is not clear, ask here.

You might want to put your server names into an array first.

Read also

Code:
perldoc -f scalar
and, at all,

Code:
perldoc perlop
and pay attention to scalar and array.
 
Old 05-25-2010, 02:06 AM   #3
webhope
Member
 
Registered: Apr 2010
Posts: 184

Rep: Reputation: 30
I think you can use sort or reverse function:
Code:
@arr = ("Tango", "India", "Hotel", "Golf", "Alfa", "Zulu", "Romeo", "Foxtrot", "Juliet", "Beta", "Yenkey", "Kilo", "Papa", "November", "Lambda", "Whisky", "Mike", "Delta", "Quebec", "Sierra");
@sorted = sort { $a <=> $b } @not_sorted; # numeral order
@sorted = sort { $a cmp $b } @not_sorted; # alphabetical order
@sorted = sort { $b cmp $a } @not_sorted; # reverse order
@sorted = sort(@arr); # asc.
@reverse = reverse(@arr); # reverse
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to get the total arguments in perl?? ashok.g Programming 22 01-12-2010 08:33 AM
[Perl] Sort a file by column Kunsheng Programming 4 04-24-2009 09:09 AM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 10:12 AM
Total newb of the SuSE 10.2 sort capt_coaldale LinuxQuestions.org Member Intro 1 07-27-2007 02:48 AM
Hash-sort like in PERL in C++ nyk Programming 4 06-11-2004 08:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration