Showing posts with label perl_basics. Show all posts
Showing posts with label perl_basics. Show all posts

Jun 11, 2013

Shift Unshift in Perl

Shift/Unshift => It works at the start of the array

Shift     => It removes element from start of an array
Unshift => It appends element to start of an array

use strict;
use warnings;

my @names = ("Foo", "Bar", "Baz");

print "\n Array : " . Dumper(\@names);

my $first = shift @names;     #Shift - removes element at the start of the array

print "$first";               #Foo
print "\n After Shift : " . Dumper(\@names);   #Bar Baz

unshift @names, "Moo";         # UnShift - adds element at the start of the array
print "\n After UnShift : " . Dumper(\@names);   # Moo Bar Baz  

Push Pop in Perl

Push/Pop      => It works at the end of the array
Push => Appends an element to end of an array
Pop  => Deletes an element from end of an array

@myNames = ('Larry', 'Curly');
push(@myNames, 'Moe');     #It adds 'Moe' at the end of the array

O/P: ('Larry', 'Curly', 'Moe')


@myNames = ('Larry', 'Curly', 'Moe');
$oneName = pop(@myNames);  #It removes 'Moe' from the end of the array

O/P: ('Larry', 'Curly')  

Delete an entry in Array and Hash in Perl

Let's discuss about deleting an element in array and hash respectively.

Deleting an element from an array:
use strict;
use warnings;

use Data::Dumper;

my @array = qw/10 20 30 40 50/;

print "\n Before Deleting :" . Dumper(\@array);

delete $array[2];

print "\n After Deleting :" . Dumper(\@array); 

Output:
Before Deleting :$VAR1 = [
          '10',
          '20',
          '30',
          '40',
          '50'
        ];

 After Deleting :$VAR1 = [
          '10',
          '20',
          undef,
          '40',
          '50'
        ];
 


Deleting an element from Hash:
use strict;
use warnings;

use Data::Dumper;

my %hash = ( a => 10, b => 20, c => 30, d => 40);

print "\n Before Deleting : " . Dumper(\%hash);

delete $hash{a};

print "\n After Deleting : " . Dumper(\%hash);

Output:
Before Deleting : $VAR1 = {
          'c' => 30,
          'a' => 10,
          'b' => 20,
          'd' => 40
        };

 After Deleting : $VAR1 = {
          'c' => 30,
          'b' => 20,
          'd' => 40
        };
  


May 29, 2013

Perl one liners

Most popular Perl One Liners

Sort File:
perl -e 'print sort {lc($a) cmp lc($b)} <>' input.txt > output.txt

Print Reverse of File:
perl -e 'print reverse <>' input.txt

Substitute the occurrences in a file:
perl -pi -e 's/foo/bar/g' input.txt      (It won't take backup)
perl -pi.bak -e 's/foo/bar/g' input.txt  (It will take backup)

Remove Duplicates and print the contents of a File :
perl -ne '$H{$_}++ or print $_' testfile.txt

Don't confuse with -n and -p switches
-n switch
  The 'n' switch will automatically add a while loop in your one-liner.
  This will help you to process each input line one by one. Each input line will be in '$_'
-p switch
  The 'p' switch is similar to 'n' switch (while loop around program) but it has an advantage of printing the line automatically.


You can use the following perl one lines from unix command line
(Reference: http://www.alfoos.com/perl-one-liners-howto/)

-e switch 
Allows you to pass a script to STDIN
Allows you to provide the program as an argument rather than in a file.
You don't want to have to create a script file for every little Perl one-liner.

# command-line that reverses the whole file by lines
         perl -e 'print reverse <>' input.txt

Note:
<> in list context returns all the lines in the file

How to Sort a file which contains strings (without case) in Ascendig order
perl -e 'print sort {lc($a) cmp lc($b)} <>' input.txt > output.txt

How to Sort a file which contains strings (without case) in Descendig order
perl -e 'print sort {lc($b) cmp lc($a)} <>' input.txt > output.txt

How to Sort a file which contains strings (with case) in Ascending Order
perl -e 'print sort {$a cmp $b} <>' input.txt > output.txt

How to Sort a file which contains strings (with case) in Descending Order
perl -e 'print sort {$b cmp $a} <>' input.txt > output.txt

How to Sort a file which contains numbers (Ascending Order)
perl -e 'print sort {$a <=> $b} <>' input.txt > output.txt

How to Sort a file which contains numbers (Decending Order)
perl -e 'print sort {$b <=> $a} <>' input.txt > output.txt

Simple Sort:
perl -we 'print sort <>' input.txt > output.txt

Sort lines by their length
perl -e 'print sort {length $a <=> length $b} <>' input.txt

-n switch
   The 'n' switch will automatically add a while loop in your one-liner.
   This will help you to process each input line one by one. Each input line will be in '$_'
 
   perl -ne 'print' input.txt   //Correct
   or
   perl -ne 'print $_' input.txt  //Correct

   perl -e 'print $_' input.txt  //Wrong

   # Delete first 10 lines 
   perl -i.old -ne 'print unless 1 .. 10' input.txt
     
   # Just lines 15 to 25 
   perl -ne 'print if 15 .. 25' input.txt
 
   Remove all blank lines of a file (-i will write into same file)
   perl -ni -e 'print unless /^$/' input.txt
 
   Remove all blank lines of a file with backup file
   perl -ni.bak -e 'print unless /^$/' input.txt
 
 
-p switch
   The 'p' switch is similar to 'n' switch (while loop around program) but it has an advantage of printing the line automatically.
 
   perl -pi -e 's/foo/bar/' *.txt
 
   perl -pe 'print' input.txt  //The lines are printed twice, once by 'p' switch and once by 'print'.

   perl -pi -e 's/foo/bar/g' input.txt (It won't take backup)
 
   perl -pi.bak -e 's/foo/bar/g' input.txt  (It will take backup)
 
-i switch 
   The 'i' switch is to do the in-line editing of file. As you can see all the one-liners above prints to STDOUT.
   Using 'i' switch you can print it to the same file from which you are reading.
   When you use 'i' switch you will not see the output in STDOUT but the output will be printed to the reading file itself.
   Modifies your input file in-place (making a backup of the original).
   Handy to modify files without the {copy, delete-original, rename} process.

   perl -i -pe 's/foo/bar/;' input.txt  //modifies in the same file (don't print to STDOUT)

   You can also pass an extension to 'i' switch.
   This will create a backup of your original file with the given extension before editing the original file
 
   perl -i.bk -pe 's/foo/bar/;' input.txt  //This take the backup

   In Perl, how to do you remove ^M from a file?
   perl -p -i -e 's/\r\n$/\n/g' file1.txt file2.txt  

-M switch 
    Although it is possible to use a -e option to load a module, Perl gives you the -M option to make that easier.

        perl -MData::Dumper -e 'my %hash = ("a" => 10, "b" => 20, "c" => 30);  print "\n Dump Value: " . Dumper(\%hash)'
perl -MCGI -e 'print "$CGI::VERSION \n"'
perl -MData::Dumper -e 'print "$Data::Dumper::VERSION \n"'

-l switch
    The 'l' switch is for processing the line terminator. As you know the default line terminator is '\n'.
You can manage the line terminator using the 'l' switch. You need to pass an octal value along 'l'.
First it does a chomp of the input records which removes that character.
Second, when doing a print it adds that character to the end of each line (by setting the $\ output record separator).
If you do not pass any octal value then '\n' will be assumed.

perl -ne 'print $_." appended to line"' testfile.txt
This will append " appended to line" in second line, this is because we havnt added 'l' switch and the string was added after '\n'.

perl -lne 'print $_." appended to line"' testfile.txt
This will " appended to line" correctly, this is because the 'l' switch removes the '\n' first then append the string and then add '\n' before printing

-a switch 
    Awk commands
    echo "Hello World" | awk '{print $2}'
    echo "Hello guys" | perl -lane 'print $F[1]'

-w switch 
   It is the same as use warnings

-d switch
   for debugging

Mar 24, 2013

Compare Numbers/Strings in Perl

Today we discuss about comparing numbers/strings in Perl.


Compare Numbers:

Syntax : 
$a == $b (Equal check for numbers)
$a != $b (Not Equal check for numbers)

compare_numbers.pl
#!/usr/bin/perl
use strict;
use warnings;

my $num1 = 100;
my $num2 = 200;

if ($num1 == $num2) {
    print "Equal\n";
} else {
    print "Not Equal\n";
}

O/P:
Not Equal

Compare Strings:

Syntax : 
$a eq $b (Equal check for strings )
$a ne $b (Not Equal check for strings)

compare_strings.pl
#!/usr/bin/perl
use strict;
use warnings;

my $string1 = 'one';
my $string2 = 'two';

if ($string1 eq $string2) {
    print "Equal\n";
} else {
    print "Not Equal\n";
}

O/P:
Not Equal


Mar 23, 2013

Create Log File in Perl


Today we discuss about how to create a log file in Perl, Logging is always part and parcel in developing code.

Here in the below mentioned example : 
1) We are trying to read the lines from the file in the path 'C:\Perl\data.txt'
2) While reading the lines, in case of any errors, we are capturing (means we are writing to log file) to log file 'C:\Perl\log.txt'
3) You can either use write/append mode to log file based on your requirement.
4) Write (">") mode : You write the content to a new log file
5) Append (">>") mode : if Log File already exists, you append the content to the existing file

log_test.pl

#!/usr/bin/perl

use strict;
use warnings;

my $log_file_path = 'C:\Perl\log.txt';
my $read_file_path = 'C:\Perl\data.txt';

#use the ">"  symbol to write to new file (Write mode)
#use the ">>" to append to the file (Append mode)
open (LOG_FILE, ">$log_file_path"); 

open (READ_FILE, "$read_file_path"); 
#"READ_FILE" is File Handle

unless (-e $read_file_path) {
    print LOG_FILE "\n Read File Doesn't Exist!!! : "  . $read_file_path;
    exit;


#use the ">"  symbol to write to new file (Write mode)
#use the ">>" to append to the file (Append mode)
while my $each_line (<READ_FILE>) {
     print "\n Each Line: :" . $each_line;
}

print LOG_FILE "\nTesting Writing into Perl file";
print LOG_FILE "\nWriting more content 111";
print LOG_FILE "\nWriting more content 222";

close (READ_FILE); 
close (LOG_FILE);

O/P:
log.txt (In case file data.txt is present in the path)
Testing Writing into Perl file
Writing more content 111
Writing more content 222

log.txt (In case file data.txt is not present in the path)
Read File Doesn't Exist!!! : log.txt


Chop Vs Chomp

Now, we discuss about Chop & Chomp functionality in Perl

Chop:
It removes the last character of the string completely (even if the last character is new line or a character)

Chomp:
It only removes the last character if it is a newline. 
Chomp is more useful when we are reading the files to trim the new line characters


Chop: It removes the last character of the string completely

E.g., chop_test.pl

#!/usr/bin/perl
use strict;
use warnings;

$name = "Mother Teresa";
print "Name before chop: " . $name;  
chop($name); 
print "Name after chop: " . $name;  

O/P:
Name before chop : Mother Teresa
Name after chop    : Mother Teres


Chomp: It only removes the last character if it is a newline. 

E.g., chomp_test.pl

#!/usr/bin/perl
use strict;
use warnings;

$name = "Mother Teresa\n";
print "Name before chomp: " . $name;  
chomp($name); 
print "Name after chomp: " . $name;  

O/P:
Name after chomp: Mother Teresa

Name after chomp: Mother Teresa

Write to File in Perl


Now, we discuss about writing content to a file

The following code snippet explains
1) Write (">") mode : You write the content to a new file
2) Append (">>") mode : File already exists, you append the content to the existing file
3) You can either define Write or Append mode based on your requirement
4) Define a file read handle (FILE_WRITE), it's can be any user defined name
5) Write the contents to file

file_write.pl

#!/usr/bin/perl

use strict;
use warnings;

my $filename = 'C:\Perl\data_write.txt';

#use the ">"  symbol to write to new file (Write mode)
#use the ">>" to append to the file (Append mode)
open (FILE_WRITE, ">$filename");
#"FILE_WRITE" is File Handle

print FILE_WRITE "\nTesting Writing into Perl file";
print FILE_WRITE "\nWriting more content 111";
print FILE_WRITE "\nWriting more content 222";
close (FILE_WRITE); 

#use the ">"  symbol to write to new file
#use the ">>" to append to the file


Output: 
data_write.txt
Testing Writing into Perl file
Writing more content 111
Writing more content 222



Reading File in Perl


Let's discuss about reading a file in Perl with an example.

The following code snippet 
1) Checks whether the file (data_read.txt) we are going to read is present or not 
2) Define a file read handle (FILE_READ), it's can be any user defined name
3) Loop through the file handle and read line line from the file
2) Chomp each & every line (to remove the new-line characters (e.g., \n)
4) Print the contents of the file line by line

E.g., 
data_read.txt
Mother Teresa
Nelson Mandela
Abraham Lincoln


file_read.pl

#!/usr/bin/perl

use strict;
use warnings;

my $filename = 'C:\Perl\data_read.txt';

#Check if file is present in the path or not
unless (-e $filename) {
    print "\n File Doesn't Exist!" . $filename;
    exit;


#"FILE_READ" is File Handle
open (FILE_READ, $filename); 

while (my $each_line = <FILE_READ>) {
   
   #"chomp" removes the new lines characters like "\n" at the end of each line 
   chomp $each_line;   
   
   print $each_line . "\n";
}

close (FILE_READ); 



Output:
Mother Teresa
Nelson Mandela
Abraham Lincoln


For Vs Foreach in Perl


Today we discuss about the difference between For & Foreach with simple examples:

FOR Syntax:
Syntax :for (initialization; test; re-initialization) BLOCK


#!/usr/bin/perl
use strict;
use warnings;

print "\nStart For Loop"; 

for ($count = 1; $count <= 5; $count++) {
  print "\nEach Element : " . $count;
}
print "\nEnd For Loop"; 


O/P:
Start For Loop
Each Element : 1
Each Element : 2
Each Element : 3
Each Element : 4
Each Element : 5
End For Loop


Foreach
The foreach keyword is actually a synonym for the "for" keyword, so we can use "foreach" (more readability or brevity)


#!/usr/bin/perl
use strict;
use warnings;

@array = ("Mother Teresa", "Abraham Lincoln", "Nelson Mandela");
foreach my $each (@array) {
  print "\nEach Element : " . $each;
}


O/P:
Each Element : Mother Teresa
Each Element : Abraham Lincoln
Each Element : Nelson Mandela


For Loop in Perl


FOR Loop Syntax in Perl:

Syntax: 
for (initialization; test; re-initialization) BLOCK


#!/usr/bin/perl

use strict;
use warnings;

print "\nStart For Loop"; 

for ($count = 1; $count <= 5; $count++) {
  print "\nEach Element : " . $count;
}
print "\nEnd For Loop"; 

O/P:
Start For Loop
Each Element : 1
Each Element : 2
Each Element : 3
Each Element : 4
Each Element : 5
End For Loop



Jun 22, 2008

While loop in Perl

We can loop through For/Foreach/While in Perl.

We can discuss while loop now with the following example.



use strict;
use warnings;
use Data::Dumper;

my @names = ("Mother Teresa", "Abraham Lincoln", "Winston Churchill", "Mahathma Gandhi");

print "\n Looping While using counter";

my $i=0;

print "\n Scalar Array Size " . scalar(@names) . "\n";

while ($i <= $#names) {
   print "\n Looping ... " . $names[$i] . "\n";
   $i++;
}

print "\n";
  

Output :
 Looping While using counter
 Scalar Array Size 4

 Looping ... Mother Teresa

 Looping ... Abraham Lincoln

 Looping ... Winston Churchill

 Looping ... Mahathma Gandhi
 


Please refer to other topics in Perl like :
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


Hash in Perl

Hashes are like named key value pairs.
Keys are always unique, Values can be anything.
It's best practice to use hashes, they are efficient.

 

use strict;
use warnings;
use Data::Dumper;

my %hash = (a => 10,
            e => 40,
            c => 30,
            d => 40,
            b => 20,
            a => 100);

#Print Keys
print "\n Only Keys:\n" . Dumper(keys %hash);

print "\n\n";
#Print Values
print "\n Only Values:\n" . Dumper(values %hash);


print "\n\n";
#Print Keys and Values
while ( my ($key, $value) = each %hash)
{
   print "$key => $value\n";
}

print "\n\n";



#Print Keys
print "\n Sorted Keys:\n" . Dumper(sort keys %hash);

print "\n\n";
#Print Values
print "\n Sorted Values:\n" . Dumper(sort values %hash);
  
Output:

 Only Keys:
$VAR1 = 'e';
$VAR2 = 'c';
$VAR3 = 'a';
$VAR4 = 'b';
$VAR5 = 'd';



 Only Values:
$VAR1 = 40;
$VAR2 = 30;
$VAR3 = 100;
$VAR4 = 20;
$VAR5 = 40;


e => 40
c => 30
a => 100
b => 20
d => 40



 Sorted Keys:
$VAR1 = 'a';
$VAR2 = 'b';
$VAR3 = 'c';
$VAR4 = 'd';
$VAR5 = 'e';



 Sorted Values:
$VAR1 = 100;
$VAR2 = 20;
$VAR3 = 30;
$VAR4 = 40;
$VAR5 = 40;
  


Please refer to other topics in Perl like :
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton