Variables et tableaux en Perl

Variables:

En perl, les variables n'ont pas de type comme en basic.

$i = "toto";
$i = 123;
$i = 4.5;

$variable = 123;
undef $variable;

if (defined $variable) 
{
    print "\$variable bien definie ma poule!\n";
} 
else 
{
    print "\$variable non definie.\n";
}

Tableaux:

Pour un tableau:

@tab = ( "abc", 123, 45.6 );
@tab = qw( "abc" 123 45.6 );
@tab = ();

push "toto", @tab;
Nombre d'éléments: 
print $#tab;
print scalar(@tab);

print( @tab );
print( $tab[0] );

foreach $var ( @tab )
{
   print $var;
}



@a = {0, 1, 2, 3, 4};
print "Deleting \$a[4].\n";
delete $a[4];

if (exists($a[4])) 
{
    print "Element is initialized.";
}
else
{
    print "Element is not initialized.";
}




push(@tab, "un");
push(@tab, "deux");
push(@tab, "trois");
print $tab[0];
un
# tri
@sorted = sort @tab;

@tab= ("un", "deux", "trois");
$variable = pop(@tab);
print $variable;
trois

@tab = ("un", "deux", "trois");
$variable = shift(@tab);
print $variable;
un

unshift @tab, "trois";
unshift @tab, "deux";
unshift @tab, "un";
print $tab[0];
un


Entree standard:
@lignes = <STDIN>; 

Les Hashs

http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/

%trad = ( 'january'  ⇒ 'janvier',
          'february' ⇒ 'fevrier',
          'march'    ⇒ 'mars' );
%trad = ();
$trad{ 'april' } = 'avril';

print "$trad{'january'}\n";

@liste_clef = keys(%trad);
@liste_valeurs = values(%trad);
($clef,$valeur) = each(%trad); 

delete($trad{'february'});


print map "$_ ⇒ $trad{$_}\n", keys %trad;


if (exists($trad{'january'})) 
{
    print "ok pour janvier.";
} 
else 
{
    print "perdu!";
}


while( ($clef, $valeur) = each(%trad)) 
{
    print "$clef ⇒ $valeur\n";
}


$h1{fruit} = apple;
$h1{sandwich} = hamburger;
$h1{drink} = bubbly;

$h2{cake} = chocolate;
$h2{pie} = blueberry;
$h2{'ice cream'} = pecan;

# merge de hash ( ou copie?)
%h3 = (%h1, %h2);
print $h3{'ice cream'};