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>




Sunday, July 24, 2011

To join all the content of the file into a scalar perl


Content of the list.txt:

hello gopal,how are you,
hello james,how are you
hello,how are you,
hello Thomas,how are you,
hello Manjula,how are you,
hello karthik,how are you

Content of the Perl file:

$ddd=getfile("list.txt");
print $ddd;
 sub getfile {
          my $filename = shift;
          local *F;
          open F, "< $filename" or die "Couldn't open `$filename': $!";
          local $/ = undef;           # Read entire file at once
          $contents = <F>;            # Return file as one single `line'
          close F;
          return $contents;
        }

output:


G:\perl_programs>perl perl_filehandle.pl
hello gopal,how are you,
hello james,how are you
hello,how are you,
hello Thomas,how are you,
hello Manjula,how are you,
hello karthik,how are you
G:\perl_programs>

Analysis:
Here the entire content of the file is joined into a scalar by changing the input record separator("$/")  to be undef which is by default to "\n".By using the local scope to the record separator the change is visible only in the subroutine getfile.