Introduction to Perl

Introduction of the basic data structures and grammars in Perl

Posted by Yingshan Li on November 8, 2020

Data structure

Scalar

Creation:

$age = 25;             # numeric
$name = "runoob";      # string
$string = 'This is
a multiline
string';     #multiline string

Operations: Context is everywhere in perl

	$str = "hello" . "world";       # Concatenates strings.
	$num = 5 + 10;                  # adds two numbers.
	$mstr = $str * $num		#$num repeats of $str

Escape Character:

\n, \\, \t ...

Array

Creation

	@ages = (25, 30, 40);             
	@names = ("google", "runoob", "taobao");

Accessing elements

	print "$ages[1]\n";
	print "$ages[2]\n";
	print "$names[0]\n";
	print "$names[1]\n";
	print "$ages[-1]\n";#Selecting from the end

Array Size

	$size = @array;

Adding and removing elements

	@coins = ("Quarter","Dime","Nickel");
	push(@coins, "Penny");
	pop(@coins);
	shift(@coins);
	unshift(@coins, "Dollar");

Slicing

	@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
	@weekdays = @days[3..5];

replacing Array Element

	@nums = (1..20);
	@num[3] = 99
	splice(@nums, 5, 4, 21..24); 

Transform String to Arrays

	$var_string = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens";
	@string = split('-', $var_string);

Transform Arrays to String

	$string1 = join( '-', @string );

Sorting Array

	@foods = qw(pizza steak chicken burgers);
	@foods = sort(@foods);

Hash

Creating Hashes

	%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
	$data{'Lisa'} = 30;
	$data{'Kumar'} = 40;

Accessing Hash Elements

	print "$data{'Lisa'}\n";

Extracting Keys and Values

	@names = keys %data;
	@ages = values %data;

Checking for Existence

	if( exists($data{'Lisa'}) ){
	   print "Lisa is $data{'Lisa'} years old\n";
	}

Getting Hash Size

	$size = @name;

Delet elements

	delete $data{'Lisa'};

Multi-layer hash

open (TAXONOMY, "rankedlineage.dmp") or die "Can't open input: $!\n";
open (HOMOLOGY, "nonphage.genomic.unique-blastn-all-3-virus-hits-complete-sortByVirusThenByHit-head" . ".txt") or die "Can't open input: $!\n";
open (OUT, ">Homology-prediction" . ".tsv") or die "Can't open input: $!\n";

while ($line = readline(TAXONOMY)) {
	chomp $line;
	@info = split /\|/, $line;
	# ///
	s{^\s+|\s+$}{}g foreach @info;
	$domain = $info[9];
	$kingdom = $info[8];
	$name = $info[1];
	$taxid = $info[0];
	
	$hash_taxonomy{$taxid}{name} = $name;
	$hash_taxonomy{$taxid}{domain} = $domain;
}
	
while ($line = readline(HOMOLOGY)) {
	chomp $line;
	@info = split /\t/, $line;
	$virus_id = $info[1];
	$virus_name=$info[2];
	$hit_id = $info[6];
	$hit_name = $info[7];
	$score = $info[5];

	$hash{$virus_id}{$hit_id}{hit_name} = $hit_name;
	$hash{$virus_id}{$hit_id}{score_sum} = $score;
	$hash{$virus_id}{$hit_id}{domain} = $hash_taxonomy{$hit_id}{domain};

	$hash_virus{$virus_id}{name} = $virus_name;

	if (($virus_id eq $virus_id_pre) and ($hit_id eq $hit_id_pre)) {
		$hash{$virus_id}{$hit_id}{score_sum} += $score;
	} else {
		$hash{$virus_id}{$hit_id}{score_sum} = $score;
	}
	$virus_id_pre = $virus_id;
	$hit_id_pre = $hit_id;
	
}

foreach $virus (sort keys %hash) {
	foreach $tax (@domain_uniq) {
		foreach $host (sort keys %{$hash{$virus}}) {
			if ($hash{$virus}{$host}{domain} eq $tax) {
				$prediction{$virus}{domain}{$tax}{score} += $hash{$virus}{$host}{score_sum};
			}
		}
	}
}

foreach $virus (sort keys %hash) {
	print OUT "$hash_virus{$virus}{name}:"."$virus\t";
	foreach $tax (sort {$prediction{$virus}{domain}{$b}{score} <=> $prediction{$virus}{domain}{$a}{score}} keys %{$prediction{$virus}{domain}}) {
		print OUT "$tax:"."$prediction{$virus}{domain}{$tax}{score}\|";
	}
	print OUT "\n";
}

close HOMOLOGY;
close TAXONOMY;
close OUT;

Control flow

Conditional statement

$a = 2
print "It's positive\n" if $a > 0;
print "It's positive" unless $a < 0;
$status = ($a >0) ? "It's positive" : "It's nagative";

Loops: Similar to all other languages

for ( init; condition; increment ){
   statement(s);
}

for( $a = 0; $a < 10; $a = $a + 1 ){
    print "$a\n";
}

while( $a < 20 ){
   printf "$a\n";
   $a = $a + 1;
}

FILE IO

open(DATA1, "<file.txt") or die "file.txt can't be open, $!";
open(DATA2, ">file.txt") or die "file.txt can't be open, $!";
while ($line = readline(DATA1)) {
	print DATA2 $line;
}
close DATA