Navigation rapide : Lundi / Mardi / Mercredi / Jeudi / Vendredi Mémos : Perl / Python / Ruby

Fonctions Perl utiles

chaînes de caratères

chomp
length substr
printf sprintf
split join
$s = "azerty\n" ;
chomp($s) ; print $s ;           # "azerty"
 
$s = "azerty" ;
chomp($s) ; print $s ;           # "azerty"
$s = "azerty" ;
print length($s) ;            # 6
print substr($s,1,2) ;        # ze
$s = "l'histoire l'a amusée" ;|
@l = split(/ +/,$s) ;
print join(" | ",@l) ;           # @l = (l'histoire | l'a | amusée)
@l = split(/[ ']+/,$s) ;
print join(" | ",@l) ;           # @l = (l | histoire | l | a | amusée)
@l = split(//,$s) ;            #
print join("|",@l) ;           # @l = (l|'|h|i|s|t|o|i|r|e| |l|'|a| |a|m|u|s|é|e)

listes

push pop unshift shift
sort reverse
@l = (1,9,4,6,2) ;
print sort @l  ;                    # (1 2 4 6 9)
print sort { $a%3 <=> $b%3 } @l ;   # (6 9 1 4 2)

hashes

keys values each
delete
exists
%h = ( un=>1, deux=>2, trois=>3 ) ;
sort keys %h ;                                            # (deux trois un)
sort { substr($a,1,1) cmp substr($b,1,1)} keys %h ;       # (deux un trois)
 
print exists $h{deux} ;                                   # vrai
delete $h{deux} ;                                         # %h = ( un=>1, trois=>3 ) ;
print exists $h{deux} ;                                   # faux
 
while (($k,$v)=each(%h)) { print "$k→$v " ; }             # trois→3 un→1

$Id: memoperlfunction.txt 551 2012-05-20 07:17:51Z jaclin $

memoperlfunction.txt · Last modified: 2012/05/20 09:17 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki