Perl is a case sensitive language.
Starting line of every perl program
#!/usr/local/bin/perl
Printing text:
print 'hello world';
When printing from a variable use double quotes instead of single quotes. Information within the single quotes are interpreted as is.
print "$var1";
Declare and assign a variable. This variable is known as a scalar variable. Scalar variables are simple variables containing only one element--a string, a number, or a reference. Strings may contain any symbol, letter, or number. Numbers may contain exponents, integers, or decimal values.
$var1 = 1;
$var1 = 'hello world';
Declare an Array:
@food = ("rice", "eggs", "orange");
Accessing a portion of an array
print "$food[1]"; //this here would print eggs with reference to the above example
Finding length of array just involves redifing the array as a scalar variable. For instance
@food = ("rice", "eggs", "orange");
print "$3"; // This would output '3'
Add and Remove elements from an array
- push() - adds an element to the end of an array.
- unshift() - adds an element to the beginning of an array.
- pop() - removes the last element of an array.
- shift() - removes the first element of an array.
Concatanate two string variables;
print $string.$linebreak;
Opening a file and printing its contents like the unix program 'cat'
$file_path = '/root/myfile.txt';
open (file1, "$file_path");
@mydata =
close(file1);
print @mydata;
Formating Characters
http://www.tizag.com/perlT/perlstrings.php
Regular Expression Cheat Cheets
http://www.cs.tut.fi/~jkorpela/perl/regexp.html
Using substrings.
To use substr() to grab a substring, you need to give it both a string variable to pick something out of and an offset (which starts at 0). The first argument of substr() is the string we want to take something from and the second argument is the offset, or where we want to start at. Substr function can take a third and forth argument, third being the length and forth being a replacement string value.
$mystr = "hello world";
$mystr1 = substr($mystr, 2);
$mystr2 = substr($mystr, 2, 3);
$mystr3 = substr($mystr, 6, 5, "there");
print "$mystr1"; // this would print 'llo world'
print "$mystr2"; // this would print 'llo'
print "$mystr"; // this would print 'hello there'. Note we are printing $mystr and not mystr3 here
Transforming strings into arrays with split function
$mystr = 'the/boy/walked/fast';
@myarr = split('/', $mystr);
print "@myarr"; // this prints 'the boy walked fast'
Likewise we can join elements of an array into a scalar string.
@array = ("David","Larry","Roger","Ken","Michael","Tom");
@array2 = qw(Pizza Steak Chicken Burgers);
@array = ("a") x 10;
print "@array"; //would print the character 'a' 10 times
# JOIN 'EM TOGETHER
$firststring = join(", ",@array);
$secondstring = join(" ",@array2);
Sorting arrays
@myarr = ("chicken" , "eggs", "apples");
@myarr = sort(@myarr);
Conditions and loops are Similar in syntax to C.
[While loop]
$a =
while ($a ne "kill")
{
print "wrong";
$a =
}
print "Correct";
[For loop]
" for ($x = 0; $x < style="color: rgb(255, 0, 0);">{
print "$x\n";
}
[until statement]
$a = 1;
do
{
print $a;
$a++
while ($a <>
[If statement]
" $a = "hello"
if (length ($a) > 3)
{
print "more than 3 characters";
}
[RE expression] using =~ or !~
$word = "Hello my good friend";
if ($word =~ /my/)
{
print "found the word: my"
exit;
}
print "Not found";
The opposite of the above would be to use '!~' instead of '=~'.
Substitution/replacement:
$word = "canada states";
$word =~ s/canada/United/ //replaces only the first occurance of the string canada with United
$word =~ s/canada/United/g //the addition of the g in the end would replace all occurances of the string. It represents global change.
$word =~ s/[Hh][Oo][Pp][Ee]/Hope/g //This pretty much ignores the case. The next example is a better way to do this
$word =~ s/CanADa/Canada/gi // The 'i' in the end ignores case
$search = "the";
s/$search/xxx/g;
will replace every occurrence of the with xxx. If you want to replace every occurence of there then you cannot do s/$searchre/xxx/ because this will be interpolated as the variable $searchre. Instead you should put the variable name in curly braces so that the code becomes $search = "the";
s/${search}re/xxx/;
Character Translation
$a = "abc";
$a =~ tr/abc/boy/; // will trnaslate a to a b, b to an o and c to a y
print $a; // will print boy
$count = ($a =~ tr/*/*/); //the statement here counts the number of asterisks in the $sentence variable and stores that in the $count variable.
However, the dash is still used to mean "between". This statement converts $_ to upper case. tr/a-z/A-Z/;
Sorting Arrays of words
@array1 = ("orange","yellow","Red","green,","blue");
@sorted = sort(@array1); //Sorts in alphabetical order
@sorted_reversed = sort {$b cmp $a} (array1); //sorts in reverse order
Sorting arrays of numbers
@array1 = (5,7,2,4,1,8,6);
@sorted = sort (@array1); //sorts in alphabetical order
@sorted_reversed = sort {$b <=> $a} (@array1); //sorts in reverse order
Key Functions to remember
my $position = index($longString, $shortstring); //returns the position of a character or substring in a string
splice (@myarr, 2 , 3); // If you have an array of 7 elements, this function reads all the 5 elements and starting from with position two(which is actually the third position, as the first element starts with a 0) cuts the next three elements.
No comments:
Post a Comment