LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: sort and get total (https://www.linuxquestions.org/questions/programming-9/perl-sort-and-get-total-809280/)

noir911 05-21-2010 06:58 AM

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);


Sergei Steshenko 05-21-2010 07:09 AM

Quote:

Originally Posted by noir911 (Post 3976478)
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.

webhope 05-25-2010 02:06 AM

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



All times are GMT -5. The time now is 03:08 AM.