Wednesday, October 5, 2011

Perl - [Function] (Sort)


  • Number
#!usr/bin/perl
use strict;
use warnings;

my @blog = (1,7,6,34,2,46,7);

print "disorderly\n";
print join (" ",@blog);

@blog = sort @blog;
print "\norderly - the first number\n";
print join (" ",@blog);

print "\norderly - the complete number( descending order )\n";
@blog = sort {$b <=> $a} @blog;
print join (" ",@blog);

print "\norderly - the complete number( ascending order )\n";
@blog = sort {$a <=> $b} @blog;
print join (" ",@blog);


Result 

$ perl blog.pl 



disorderly
1 7 6 34 2 46 7
orderly - the first number
346 6 7 7
orderly - the complete numberdescending order )
46 34 7 7 6 2 1
orderly - the complete number( ascending order )
1 2 6 7 7 34 46

-------------------------------------------------------------------------------------------------------------------------------
  • String 

#!usr/bin/perl
use strict;
use warnings;

my @blog = ('oi','tim','claro','vivo','nextel');
print "Disorderly : \n";
print join ("\t",@blog);

@blog = sort @blog;
print "\nOrderly : \n";
print join("\t",@blog);

@blog = ('cab','abc','abb','aaa','baa');
print "\nDisorderly : \n";
print join ("\t",@blog);

@blog = sort @blog;
print "\nOrderly : \n";
print join("\t",@blog);

Result 

$ perl blog.pl 



Disorderly :
oi tim claro vivo nextel
Orderly :
claro nextel oi tim vivo

Disorderly :
cab abc abb aaa baa
Orderly :
aaa abb abc baa cab
----------------------------------------------------------------------------------------------------------------------------------


continues soon ...

0 comentários:

Post a Comment