Data Types in Perl




Paolo Marcatili - Programmazione 09-10
Agenda

> Perl Basics
> Hello World
> Scalars
> Arrays
> Hashes




                                                 2
   ...
Task Today




Paolo Marcatili - Programmazione 09-10
parsing

Parse a GO file
Extract gene names and function




                                                 4
        Pao...
Perl Basics




Paolo Marcatili - Programmazione 09-10
PERL

Practical Extraction
and Reporting Language

 Handle text files
 Web (CGI)
 Small scripts

http://www.perltutorial...
Install

Windows
http://www.activestate.com/activeperl/
Cygwin (linux emulation)


Linux / OS-X
Native




               ...
Hello World!




Paolo Marcatili - Programmazione 09-10
First script

Open an editor (e.g. gedit)

#!/usr/bin/perl -w
use strict;
use warnings;
print "Hello World!n";


Save as -...
How to run a script

Terminal -> move to the script folder

perl first.pl

or
chmod a+x first.pl <- now it is executable by
...
Scalars




Paolo Marcatili - Programmazione 09-10
Scalars
my $scalar;
$scalar=5;
$scalar=$scalar+3;
$scalar= “scalar vale $scalarn”;
print $scalar;


> scalar vale 8




  ...
Scalars - 2

 Scalar data can be number or string.
 In Perl, string and number can be used
  nearly interchangeable.

 ...
Examples
#floating-point values
my $x = 3.14;
my $y = -2.78;

#integer values
my $a = 1000;
my $b = -2000;

my $s = "2000";...
Operations
my $x = 5 + 9; # Add 5 and 9, and then store the result in $x
$x = 30 - 4; # Subtract 4 from 30 and then store ...
Operations - 2
my $x = 3;
my $c = "he ";
my $s = $c x $x; # $c repeated $x times
my $b = "bye";
print $s . "n"; #print s a...
Arrays




Paolo Marcatili - Programmazione 09-10
boxed scalars




   Scalar       Array




                        18
array - 1


("Perl","array","tutorial");
(5,7,9,10);
(5,7,9,"Perl","list");
(1..20);
();




                             ...
array - 2

my @str_array=("Perl","array","tutorial");
my @num_array=(5,7,9,10);
my @mixed_array=(5,7,9,"Perl","list");
my ...
operations
my @int =(1,3,5,2);
push(@int,10); #add 10 to @int
print "@intn";

my $last = pop(@int); #remove 10 from @int
p...
on array

my @int =(1,3,5,2);

foreach my $element (@int){
print “element is $elementn”;
}

my @sorted=sort(@int);
foreach...
Hashes




Paolo Marcatili - Programmazione 09-10
Hashes
> Hashes are like array, they store collections of
  scalars
  ... but unlike arrays, indexing is by name (just lik...
Hashes continued ...
> Adding a new key-value pair
       $phoneDirectory{“Shirly”} = 7267975

   > Note the $ to specify ...
Hashes and Foreach
> Foreach works in hashes as well!

   foreach $person (keys (%phoneDirectory) )
      {
     print “$p...
Hashes and Sorting
> The sort function works with hashes as well
> Sorting on the keys
   foreach $person (sort keys %phon...
Hash and Sorting cont...
> Sorting by value

   foreach $person (sort {$phoneDirectory{$a} <=>
      $phoneDirectory{$b}} ...
Exercise

> Chose your own test or use wget

> Identify the 10 most frequent words

> Sort the words alphabetically

> Sor...
Counting Words
my %seen;
  my $l=“Lorem ipsum”;
  my @w=split (“ “, $l);# questa è una funzione nuova…
  foreach my $word ...
Upcoming SlideShare
Loading in …5
×

Data Types Master

1,510 views

Published on

Perl data types and operations: scalars, arrays and hashes

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,510
On SlideShare
0
From Embeds
0
Number of Embeds
80
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Data Types Master

  1. 1. Data Types in Perl Paolo Marcatili - Programmazione 09-10
  2. 2. Agenda > Perl Basics > Hello World > Scalars > Arrays > Hashes 2 Paolo Marcatili - Programmazione 09-10
  3. 3. Task Today Paolo Marcatili - Programmazione 09-10
  4. 4. parsing Parse a GO file Extract gene names and function 4 Paolo Marcatili - Programmazione 09-10
  5. 5. Perl Basics Paolo Marcatili - Programmazione 09-10
  6. 6. PERL Practical Extraction and Reporting Language  Handle text files  Web (CGI)  Small scripts http://www.perltutorial.org/ 6 Paolo Marcatili - Programmazione 09-10
  7. 7. Install Windows http://www.activestate.com/activeperl/ Cygwin (linux emulation) Linux / OS-X Native 7 Paolo Marcatili - Programmazione 09-10
  8. 8. Hello World! Paolo Marcatili - Programmazione 09-10
  9. 9. First script Open an editor (e.g. gedit) #!/usr/bin/perl -w use strict; use warnings; print "Hello World!n"; Save as -> first.pl 9 Paolo Marcatili - Programmazione 09-10
  10. 10. How to run a script Terminal -> move to the script folder perl first.pl or chmod a+x first.pl <- now it is executable by everyone ./first.pl <- ./ means ‘in this folder’ 10 Paolo Marcatili - Programmazione 09-10
  11. 11. Scalars Paolo Marcatili - Programmazione 09-10
  12. 12. Scalars my $scalar; $scalar=5; $scalar=$scalar+3; $scalar= “scalar vale $scalarn”; print $scalar; > scalar vale 8 12
  13. 13. Scalars - 2  Scalar data can be number or string.  In Perl, string and number can be used nearly interchangeable.  Scalar variable is used to hold scalar data.  Scalar variable starts with dollar sign ($) followed by Perl identifier.  Perl identifier can contain alphanumeric and underscores.  It is not allowed to start with a digit. 13
  14. 14. Examples #floating-point values my $x = 3.14; my $y = -2.78; #integer values my $a = 1000; my $b = -2000; my $s = "2000"; # similar to $s = 2000; #strings my $str = "this is a string in Perl". my $str2 = 'this is also as string too'. 14
  15. 15. Operations my $x = 5 + 9; # Add 5 and 9, and then store the result in $x $x = 30 - 4; # Subtract 4 from 30 and then store the result in $x $x = 3 * 7; # Multiply 3 and 7 and then store the result in $x $x = 6 / 2; # Divide 6 by 2 $x = 2 ** 8; # two to the power of 8 $x = 3 % 2; # Remainder of 3 divided by 2 $x++; # Increase $x by 1 $x--; # Decrease $x by 1 my $y = $x; # Assign $x to $y $x += $y; # Add $y to $x $x -= $y; # Subtract $y from $x $x .= $y; # Append $y onto $x 15
  16. 16. Operations - 2 my $x = 3; my $c = "he "; my $s = $c x $x; # $c repeated $x times my $b = "bye"; print $s . "n"; #print s and start a new line # similar to print "$sn"; my $a = $s . $b; # Concatenate $s and $b print $a; # Interpolation my $x = 10; my $s = "you get $x"; print $s; 16
  17. 17. Arrays Paolo Marcatili - Programmazione 09-10
  18. 18. boxed scalars Scalar Array 18
  19. 19. array - 1 ("Perl","array","tutorial"); (5,7,9,10); (5,7,9,"Perl","list"); (1..20); (); 19
  20. 20. array - 2 my @str_array=("Perl","array","tutorial"); my @num_array=(5,7,9,10); my @mixed_array=(5,7,9,"Perl","list"); my @rg_array=(1..20); my @empty_array=(); print $str_array[1]; # 1st element is [0] 20
  21. 21. operations my @int =(1,3,5,2); push(@int,10); #add 10 to @int print "@intn"; my $last = pop(@int); #remove 10 from @int print "@intn"; unshift(@int,0); #add 0 to @int print "@intn"; my $start = shift(@int); # add 0 to @int print "@intn"; 21
  22. 22. on array my @int =(1,3,5,2); foreach my $element (@int){ print “element is $elementn”; } my @sorted=sort(@int); foreach my $element (@sorted){ print “element is $elementn”; } 22
  23. 23. Hashes Paolo Marcatili - Programmazione 09-10
  24. 24. Hashes > Hashes are like array, they store collections of scalars ... but unlike arrays, indexing is by name (just like in real life!!!) > Two components to each hash entry: > Key example : name > Value example : phone number > Hashes denoted with % > Example : %phoneDirectory > Elements are accessed using {} (like [] in arrays) 24 Paolo Marcatili - Programmazione 09-10
  25. 25. Hashes continued ... > Adding a new key-value pair $phoneDirectory{“Shirly”} = 7267975 > Note the $ to specify “scalar” context! > Each key can have only one value $phoneDirectory{“Shirly”} = 7265797 # overwrites previous assignment > Multiple keys can have the same value > Accessing the value of a key $phoneNumber =$phoneDirectory{“Shirly”}; 25 Paolo Marcatili - Programmazione 09-10
  26. 26. Hashes and Foreach > Foreach works in hashes as well! foreach $person (keys (%phoneDirectory) ) { print “$person: $phoneDirectory{$person}”; } > Never depend on the order you put key/values in the hash! Perl has its own magic to make hashes amazingly fast!! 26 Paolo Marcatili - Programmazione 09-10
  27. 27. Hashes and Sorting > The sort function works with hashes as well > Sorting on the keys foreach $person (sort keys %phoneDirectory) { print “$person : $directory{$person}n”; } > This will print the phoneDirectory hash table in alphabetical order based on the name of the person, i.e. the key. 27 Paolo Marcatili - Programmazione 09-10
  28. 28. Hash and Sorting cont... > Sorting by value foreach $person (sort {$phoneDirectory{$a} <=> $phoneDirectory{$b}} keys %phoneDirectory) { print “$person : $phoneDirectory{$person}n”; } > Prints the person and their phone number in the order of their respective phone numbers, i.e. the value. 28 Paolo Marcatili - Programmazione 09-10
  29. 29. Exercise > Chose your own test or use wget > Identify the 10 most frequent words > Sort the words alphabetically > Sort the words by the number of occurrences 29 Paolo Marcatili - Programmazione 09-10
  30. 30. Counting Words my %seen; my $l=“Lorem ipsum”; my @w=split (“ “, $l);# questa è una funzione nuova… foreach my $word (@w){ $seen{$word}++; } print “Sorted by occurrencesn”; foreach my $word (sort {$seen{$a}<=>$seen{$b}} keys %seen){ print “Word $word N: $seen{$word}n”; } print “Sorted alphabeticallyn”; foreach my $word (sort ( keys %seen)){ print “Word $word N: $seen{$word}n”; } 30 Paolo Marcatili - Programmazione 09-10

×