Sunday, July 31, 2011

Extensions in perl

Extensions significantly add to the power of regular expressions instead of adding lot of metacharacters.

Commonly used extensions in perl are:
1)(?#TEXT)    - it is used for adding comments in the regular expressions.

2)(?:...)       - it is used in case when you want paranthesis to be used without saving
the matched pattern in $n

3)(?=...)    -  it is used for matching without using the specific value in the $& variable.

4)(?!...)  - it is used when you don't want the pattern in the paranthesis to follow the word before or after.

Example:
1)(?#TEXT)-
it can be used simply used it just adds a clarity to the people viewing this pattern.
$dd =~ m/\w(?#it matches a single character)\s(it matches a single character)/;

2)(?:.....)-


$dd="hello how are you";
$dd=~ m/(?:\w*)/;
print $1;

if you see the example above the $1 is supposed to match hello since "?:" is specified in parantheses it is empty.

3)(?=...)-
it can be used in the case when you want to match a pattern based on the value in the paranthesis but it should not get included in $& variable in that case this will be useful.

$dd="hello how are you";
$dd=~ m/\w*\s+(?=how)/;
print $&;

Output:
hello

In the example specified i am trying to match a word followed by "how".If i have
parathesis without "?=" the output will be "hello how".Using "?=" has done the trick to print only hello.

4)(?!....)
It is very useful extension which can be used in case where you donot want a word to follow or precede the match you are looking for.
For example: m/red(?!carpet)/ will match redsea or redfly but not redcarpet.
The detailed example can be seen in the url:
http://linux-forum-karthik.blogspot.com/2011/07/to-match-pattern-not-having-word.html



Saturday, July 30, 2011

map vs grep in perl


grep function in perl:
grep function is normally used to select the matched pattern and return it to the array

grep(EXPRESSION,@array);
grep(BLOCK,@array);

Perl file:

$"="\n";
$\="\n";
@array=(0..3,"karthik");
print "@array";
@result=grep($_=~ s/0/9/,@array);
print "array:\n@array";
print "result:\n@result";

output:

G:\perl_programs>perl perl_map.pl
0
1
2
3
karthik
array:
9
1
2
3
karthik
result:
9

G:\perl_programs>

Analysis:
In this case if you see the grep function tries to substitute 0 with 9 by moving through each element of the array.When it finds a element with value 0 it
modifies the original array and returns the replaced value to the result array.
So as you may see from the output the result array has only one value "9".

Map function in perl:
Map function is used to evaluate the given expression and return t
he result value to the array.


$"="\n";
$\="\n";
@array=(0..3,"karthik");
print "@array";
@result=map($_=~ s/0/9/,@array);
print "array:\n@array";
print "result:\n@result";


Output:

G:\perl_programs>perl perl_map.pl
0
1
2
3
karthik
array:
9
1
2
3
karthik
result:
1






G:\perl_programs>

Analysis:
In this case if you see the map function tries to substitute 0 with 9 by moving through each element of the array similar as in grep.But When it does not 
find the match it returns empty string to the result array and when it finds the match it modifies the original array with the substituted value and returns the "1" to the result array which is the return value for pass or fail for the substitution.
So as you may see from the output the result array has only one value "1" followed
by space for the values which did not match.

finding the count of a certain character in a line perl


Perl Program:

$_="great,awesome.wonderful  wonder";
$dd=tr/w//;
print "count of character w is :$dd";

Output:

G:\perl_programs>perl perl_tr.pl
count of character w is :3

G:\perl_programs>
Analysis:
Here i have used tr operator to find the count of character 'w'.tr operator is normally used to translate the characters.But in this case i have translatted 'w' into
nothing as per the pattern "tr/w//".So it just returns the no of character matched in the line.

Friday, July 29, 2011

finding the no of values in array match a pattern

grep can also be used to find no of values in a array match the pattern specified by assigning to the scalar.
using the below example:


@a=('one','Two','Three','Four','Five');
$ff=grep(/^T/,@a);
print "$ff";

Output:

G:\perl_programs>perl perl_grep.pl
2
G:\perl_programs>

Explanation:
Here the pattern searched through arrays is "^T".So here Two and Three values match Since it is assigned to a scalar it is converted to 2(no of values that match)

grep in perl


1)grep in perl can be used to search a pattern in a array and print the values that match to a array.


@a=('one','Two','Three','Four','Five');
@ff=grep(/^T/,@a);
print "@ff";


G:\perl_programs>perl perl_grep.pl
Two Three
G:\perl_programs>

Explanation:
Here the pattern searched through arrays is "^T".So here Two and Three values match and it is assigned to the array.

2)grep can also be used to find no of values in a array match the pattern specified by assigning to the scalar.
using the below example:

@a=('one','Two','Three','Four','Five');
$ff=grep(/^T/,@a);
print "$ff";

Output:

G:\perl_programs>perl perl_grep.pl
2
G:\perl_programs>

Explanation:
Here the pattern searched through arrays is "^T".So here Two and Three values match Since it is assigned to a scalar it is converted to 2(no of values that match).

3)It can also be used to process the arrays such as substitute ceratin content of array likewise


@a=('one','Two','Three','Four','Five');
$ff=grep(s/Two/zero/,@a);
print "The no of values where it was substituted:$ff"."\n";
print "The changed array:@a"."\n";

Output:

G:\perl_programs>perl perl_grep.pl
The no of values where it was substituted:1
The changed array:one zero Three Four Five

G:\perl_programs>

Explanation:
Here the grep function is used to substitute Two with zero by moving through every array content.

4)grep function can also be used to print the array content .But it is not preferred it is better to use map function for that:

@a=('one','Two','Three','Four','Five');
$ff=grep(print("$_\n"),@a);


Output:


G:\perl_programs>perl perl_grep.pl
one
Two
Three
Four
Five
G:\perl_programs>

Monday, July 25, 2011

perl array newline



#!/usr/bin/perl
$"="\n";
@array1=("hello","great","awesome","wonderful");
print "@array1";

Here the special variable $" is used which is the separator used between the list elements when an array variable is interpolated into double quotes.Normally this value is set to default as space.Here i have made this value expilictly as "\n" to make it print in a newline.

Output:

G:\perl_programs>perl perl_array_print.pl
hello
great
awesome
wonderful
G:\perl_programs>perl perl_array_print.pl


print with newline perl



$\="\n";
print "@hell";
print "marvellous";
print "great";
print "asasasasas"."ffffff"."fdifdfd";
print "awesome";

Here the special variable "$\" is used which is added as  the invisible element to end of the print statement .Normally it is empty it can be assigned "\n" if you want to print everytime in a newline.

output:

G:\perl_programs>perl perl_print.pl

marvellous
great
asasasasasfffffffdfdfd
awesome

G:\perl_programs>