Visitor

Tuesday, June 8, 2010

Sample Perl script to play with file

Script to read a file using while loop.

#!/usr/local/ActivePerl-5.8/bin/perl
if (open(MYFILE, "file1")) {
$line = ;
while ($line ne "") {
print ($line);
$line = ;
}
}

Script to write in a file

#!/usr/local/bin/perl

open (MYPIPE, "cat >hello");
print MYPIPE ("Hi, Dave! Your Perl program sent this!\n");

close (MYPIPE); 


Script to search specific word from the file.

#!/usr/local/bin/perl



print ("Word to search for $ARGV[0]\n");
$filecount = 1;
$totalwordcount = 0;
while ($filecount <= @ARGV-1)
{
unless (open (INFILE, $ARGV[$filecount]))
{
die ("Can't open input file $ARGV[$filecount]\n");
}
$wordcount = 0;
while ($line = )
{
chop ($line);
print ("Before split : $line\n");
@words = split(/ /, $line);
print ("Content is : @words\n");
$w = 1;
while ($w <= @words)
{
if ($words[$w-1] eq $ARGV[0])
{
$wordcount += 1;
}
$w++;
}
}
print ("occurrences in file $ARGV[$filecount] ");
print ("$wordcount\n");
$filecount++;
$totalwordcount += $wordcount;
}
print ("total number of occurrences $totalwordcount\n");

VBscript: Sample script for msg box with class

Class Hello_World

Public Sub Say_Hello(Name)
MsgBox "Hello, " & Name & ", welcome to " & Garden & "."
End Sub
Public Garden
End Class

Dim MyHello_World

Set MyHello_World = New Hello_World

MyHello_World.Garden = "Fountain"
MyHello_World.Say_Hello "Sachin"
MyHello_World.Garden = "Foun"