Top Banner
Data Types in Perl Paolo Marcatili - Programmazione 09-10
30

Data Types Master

Jan 15, 2015

Download

Technology

Paolo Marcatili

Perl data types and operations: scalars, arrays and hashes
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Types Master

Data Types in Perl

Paolo Marcatili - Programmazione 09-10

Page 2: Data Types Master

2

Agenda

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

Paolo Marcatili - Programmazione 09-10

Page 3: Data Types Master

Task Today

Paolo Marcatili - Programmazione 09-10

Page 4: Data Types Master

4

parsing

Parse a GO fileExtract gene names and function

Paolo Marcatili - Programmazione 09-10

Page 5: Data Types Master

Perl Basics

Paolo Marcatili - Programmazione 09-10

Page 6: Data Types Master

6

PERL

Practical Extractionand Reporting Language

Handle text files Web (CGI) Small scripts

http://www.perltutorial.org/

Paolo Marcatili - Programmazione 09-10

Page 7: Data Types Master

7

Install

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

Linux / OS-XNative

Paolo Marcatili - Programmazione 09-10

Page 8: Data Types Master

Hello World!

Paolo Marcatili - Programmazione 09-10

Page 9: Data Types Master

9

First script

Open an editor (e.g. gedit)

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

Save as -> first.pl

Paolo Marcatili - Programmazione 09-10

Page 10: Data Types Master

10

How to run a script

Terminal -> move to the script folder

perl first.pl

orchmod a+x first.pl <- now it is executable by

everyone./first.pl <- ./ means ‘in this folder’

Paolo Marcatili - Programmazione 09-10

Page 11: Data Types Master

Scalars

Paolo Marcatili - Programmazione 09-10

Page 12: Data Types Master

12

Scalars

my $scalar;$scalar=5;$scalar=$scalar+3;$scalar= “scalar vale $scalar\n”;print $scalar;

> scalar vale 8

Page 13: Data Types Master

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.

Page 14: Data Types Master

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'.

Page 15: Data Types Master

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

Page 16: Data Types Master

16

Operations - 2

my $x = 3;my $c = "he ";my $s = $c x $x; # $c repeated $x timesmy $b = "bye";print $s . "\n"; #print s and start a new line# similar toprint "$s\n";my $a = $s . $b; # Concatenate $s and $bprint $a;# Interpolationmy $x = 10;my $s = "you get $x";print $s;

Page 17: Data Types Master

Arrays

Paolo Marcatili - Programmazione 09-10

Page 18: Data Types Master

18

boxed scalars

Scalar Array

Page 19: Data Types Master

19

array - 1

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

Page 20: Data Types Master

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]

Page 21: Data Types Master

21

operationsmy @int =(1,3,5,2);push(@int,10); #add 10 to @intprint "@int\n"; my $last = pop(@int); #remove 10 from @intprint "@int\n"; unshift(@int,0); #add 0 to @intprint "@int\n";my $start = shift(@int); # add 0 to @intprint "@int\n";

Page 22: Data Types Master

22

on array

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

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

my @sorted=sort(@int);foreach my $element (@sorted){print “element is $element\n”;}

Page 23: Data Types Master

Hashes

Paolo Marcatili - Programmazione 09-10

Page 24: Data Types Master

24

Hashes> Hashes are like array, they store collections of

scalars... but unlike arrays, indexing is by name (just like inreal 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)

Paolo Marcatili - Programmazione 09-10

Page 25: Data Types Master

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”};

Paolo Marcatili - Programmazione 09-10

Page 26: Data Types Master

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/valuesin the hash! Perl has its own magic to makehashes amazingly fast!!

Paolo Marcatili - Programmazione 09-10

Page 27: Data Types Master

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 theperson, i.e. the key.

Paolo Marcatili - Programmazione 09-10

Page 28: Data Types Master

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 theorder of their respective phone numbers, i.e.the value.

Paolo Marcatili - Programmazione 09-10

Page 29: Data Types Master

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 ofoccurrences

Paolo Marcatili - Programmazione 09-10

Page 30: Data Types Master

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 occurrences\n”;foreach my $word (sort {$seen{$a}<=>$seen{$b}} keys %seen){

print “Word $word N: $seen{$word}\n”;}

print “Sorted alphabetically\n”;foreach my $word (sort ( keys %seen)){print “Word $word N: $seen{$word}\n”;}

Paolo Marcatili - Programmazione 09-10