Programming in Perl Variables Peter Verhás January 2002 Perl Variables • scalar variables • array variables • hash variables • Special variabl

Document Sample
Programming in Perl Variables Peter Verhás January 2002 Perl Variables • scalar variables • array variables • hash variables • Special variabl Powered By Docstoc
					  Programming in Perl
           Variables

 Peter Verhás
January 2002.
                       Perl Variables

• $ scalar variables
• @ array variables
• % hash variables

• Special variables, like $_ or $/
                             Using Arrays

• Using array as an whole array
  – @a = ( 1, 2, 3, )
• Using array element
  – $a[0] is scalar 1
  – (unless $[ is assigned different from 0)
• Arrays are one dimensional
  – You will learn later how to emulate multi-
    dim arrays
                              Array Slices

@foo = (1,2,3);
print $foo[1,2],"\n",@foo[1,2],"\n";

               $foo[1,2] is the same as $foo[2]
OUTPUT:        (comma operator)
3
23             @foo[1,2] is an array slice, is the
               same as ($foo[1],$foo[2])

               You can also use @foo[0..2]
                     What is hash?

• Hash is associative array
• Index can be anything not only
  number
• %hash = (1, 2, ”b”, 4); has 2
  elements
  – $hash{1}=2 and $hash{”b”}=4 but
    also can be written as
• %hash = ( 1 => 2, ”b” => 4 );
            Array Slices from hash

%foo = (1=>2,3=>4,'apple'=>'peach');
print @foo{1,3},"\n";

OUTPUT:        @foo{1,3} is a hash slice, is the same
24             as ($fo{1},$fo{2})

               It starts with @ and not %
               Assign to Array Slice

@a = ( 1,2,3,4,5,6);
print @a,"\n";
@a[0..2] = (3,2,1,0);
print @a,"\n";
                        You can assign value to a
@a[0..2] = (0,0);       slice of an array.
print @a,"\n";
OUTPUT:                 Extra elements are ignored.
123456
321456                  If there are less elements the
00456                   array gets shorter.
       Don’t program dangerous!

• $variable
• @variable
• %variable

• Are three different variables!
• And still you can have subroutine
  with the name.
                     Expression (1)

• Expression is just as in other
  programming language
• + - / * arithmetic operators
• . string concatenation
• == equality numeric comparison
• != non equal, <=, <, >, >= numeric
  comparison
• eq, ne, lt, le, gt, ge string
  comparison
                    Expression (2)

• Precedence as usually
• Use ( and ) to group sub-
  expressions
• condition ? true-exp:false-exp
• , comma operator
• = assignment operator
• op= operator assignment
  operators +=, -=, /=, ~=
                            Strings

• Interpolated and non-interpolated
  strings
$a = 'apple';
print "$a\n";
print '$a\n';
OUTPUT:
apple
$a\n
                    Multi-line strings

$a = 'zero';
$b = <<END;
this is ba: $a
END

print $b;
                   OUTPUT:
$c = <<'END';
this is ca: $a     this is ba: zero
END
print $c;          this is ca: $a

 Play with the interpolated strings putting
 expressions into it and experience what is
        interpolated and what is not!
    Simple string handling operators


• Concatenate strings:
  – $a = ”apple” . ”peach”;
• Automatic conversion
  – $a .= 555;

  OUTPUT:
  applepeach555
          Simple String Functions

$a = "apple";
print substr($a,1,2),length($a);


OUTPUT:

pp5
Thank you for your kind attention.