Monday, August 23, 2010

Difference between Windows and Unix?

Unix is much better at handling multiple tasks for a single user or for multiple users than windows.

Unix:

Multiple Users can access and use the unix systems


Windows:

Single User can access and use the system.

Wednesday, August 18, 2010

Important file tests in Perl

File Test                   Meaning

-M                           Modification age (measured in days)

-A                            Access age (measured in days)

-C                            Inode modification age (measured in days)

The shift and unshift operators

The shift and unshift operators perform the operations on the start of the array ( the left side of an array or the portion with the lowest subscripts ).

shift Operator:

@array=qw # abc def ghi #;
$element_1=shift(@array); #gets abc
$element_2=shift @array; #gets def

unshift Operator:


unshift (@array, zyx); # @array has zyx abc
unshift @array, wvu; # @array has wvu zyx abd
@other_array=1..5; # has 1 2 3 4 5
unshift @array, @other_array; #@array now has 1 2 3 4 5 wvu zyx abd

Push and Pop Operator

Push and pop functions are used to implement stack where new values are added to and removed from the right hand side of the list.

pop Operator:

Pop operator takes the last element off an array and returns it:

For example:

$array=5..9;                               #5 6 7 8 9
$last_element_1=pop(@array);   #gets 9

$last_element_2=pop @array;    # gets 8 ( pop operator works without parentheses also)

push Operator:

Push operator adds and element to the end of the array

For example:

@array=3..6;         #3 4 5 6
push(@array, 7);    #@array has 3 4 5 6 7
push @array, 8;     #@array has 3 4 5 6 7 8

Short Circuit Operator in Perl?

The logical "or" operator i.e "||" is called as Short Circuit Operator.

Perl Script to reverse a string

Program:

#!/depot/perl-5.8.3/bin/perl

my $str1="Madam i m adaM";
print "ORIGINAL_STRING: $str1 \n";
my $l=length($str1);
my $rev="";
while ($l >0) {
    $l--;
    $rev .= substr($str1,$l,1);
    }
print "REVERSED_STRING: $rev \n";

Sub routine with an array, a scalar and a string as arguments

#!/depot/perl-5.8.3/bin/perl

@arr1=(1..4);
my $count="9";
my $str1="To verify the subroutine argument list";
&dolist(\@arr1, $count, $str1);

sub dolist
   {
      print "ALL_ARGUMENTS = @_ \n";
      my @list = @{$_[0]};
      print "ARRAY  ==>  @list \n";
      my $c = $_[1];
      print "SCALAR  ==>  $c\n";
      my $string1= $_[2];
      print  "STRING  ==>  $string1 \n";

}

Perl Command Line options

-p   -  Write a perl program

-w  -   Enables warnings

-e   -   Executable code follows

-d   -   Enable diagnostics

-M  -  To load a pragma only when needed instead of loading it at the compile time.

System Variables

$$     -   Yields the process id of the perl program
$@   -    Yields the error message STDERR
$|      -   Yields the corresponding System Error
@_   -    Stores all the arguments passed to a sub routine
$&    -    Part of  the string that matches the pattern
$`      -   String before the pattern match
$'       -   String after the pattern match
$^I    -     Backup file name's extension

Find the missing element - There are 9 No's which are distinct and are within the range 1-10. How to find the missing element.

Solution 1:

#!/depot/perl-5.8.3/bin/perl

@array=(1, 2, 3, 4, 5, 6, 7, 8, 10);
foreach $n (1..10) {
if (!grep (/\b$n\b/,@array)) {
    print "$n \n";
}
}

Solution 2:

As the no's are in sequence and without repetition we can use the summation formula n (n + 1) /2.

 #!/depot/perl-5.8.3/bin/perl

@array=(1, 2, 3, 4, 5, 6, 7, 8, 10);
my $sum_of_10_consecutive_no=(10 * 11 / 2);
my $sum=0;
foreach (@array) {
    $sum += $_;
    }
print $sum_of_10_consecutive_no - $sum;

Perl script to print the below triangle pattern

OUTPUT:

1
1 2
1 2 3
1 2 3 4

Program:

#!/depot/perl-5.8.3/bin/perl

foreach (1..4) {
@a=(1..$_);
print "@a \n";
}

Given a file, print the count of all the words (OUTPUT: word --> count)

This is using two for loops so the time complexity would be n^2, where n is the no of lines in file1.



#!/depot/perl-5.8.3/bin/perl

open FH1, "file1" or die "Cannot open file file1 : $!";
@array=<FH1>;
foreach $l (@array) {
push(@arr1,split(/\s+/,$l));
}
my %hash1;
foreach $y (@arr1) {
$hash1{$w}=0;
foreach $z (@arr1) {
if ($y eq $z) {
$hash1{$y}++;
}
}
}

foreach $key (keys %hash1) {
print "$key --> $hash1{$key} \n";
}



This is using a while and a for loop, the time complexity is "n":

#!/usr/local/bin/perl

use strict;
use warnings;

my %count;
my $file = shift or die "Usage: $0 FILE\n";
open my $fh, '<', $file or die "Could not open '$file' $!";
while (my $line = <$fh>) {
        chomp $line;
        foreach my $str (split /\s+/, $line) {
             $count{$str}++;
        }
}

foreach my $str (sort keys %count) {
        printf "%-31s %s\n", $str, $count{$str};
}


USAGE: ./count.pl  txt1

OUTPUT:

Hii                             2
am                              1
are                             3
doing                           1
hii                             2
how                             2
well                            1
you                             3