winsem2013-14_cp3057_28-jan-2014_rm01_demopattern1 (1)

Upload: shravan-kumar

Post on 03-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    1/10

    #!/usr/local/bin/perl

    ######## A word-count program that handles multiple spaces between words.

    $wordcount = 0;

    $line = ;

    while ($line ne "") {

    chop ($line);

    @words = split(/ +/, $line);

    $wordcount += @words;

    $line = ;

    }

    print ("Total number of words: $wordcount\n");

    #o/p

    #$ program7_2

    #Here is some input.

    #Here are some more words.

    #Here is my last line.

    #^D

    #Total number of words: 14

    ##############################################################################

    #####

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    2/10

    ##############################################################################

    #####

    ########A simple variable-name validation program.

    print ("Enter a variable name:\n");

    $varname = ;

    chop ($varname);

    if ($varname =~ /\$[A-Za-z][_0-9a-zA-Z]*/) {

    print ("$varname is a legal scalar variable\n");

    } elsif ($varname =~ /@[A-Za-z][_0-9a-zA-Z]*/) {

    print ("$varname is a legal array variable\n");

    } elsif ($varname =~ /[A-Za-z][_0-9a-zA-Z]*/) {

    print ("$varname is a legal file variable\n");

    } else {

    print ("I don't understand what $varname is.\n");

    }

    #Enter a variable name:

    #$result

    #$result is a legal scalar variable

    ##############################################################################

    #####

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    3/10

    ##############################################################################

    #####

    ######## A program that counts the number of input lines containing the word the.

    $thecount = 0;

    print ("Enter the input here:\n");

    $line = ;

    while ($line ne "") {

    if ($line =~ /\bthe\b/) {

    $thecount += 1;

    }

    $line = ;

    }

    print ("Number of lines containing 'the': $thecount\n");

    #Enter the input here:

    #Now is the time

    #for all good men

    #to come to the aid

    #of the party.

    #^D

    #Number of lines containing 'the': 3

    ##############################################################################

    #####

    ######## A simple pattern-search program.

    print ("Enter the search pattern:\n");

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    4/10

    $pattern = ;

    chop ($pattern);

    $filename = $ARGV[0];

    $linenum = $matchcount = 0;

    print ("Matches found:\n");

    while ($line = ) {

    $linenum += 1;

    if ($line =~ /$pattern/) {

    print ("$filename, line $linenum\n");

    @words = split(/$pattern/, $line);

    $matchcount += @words - 1;

    }

    if (eof) {

    $linenum = 0;

    $filename = $ARGV[0];

    }

    }

    if ($matchcount == 0) {

    print ("No matches found.\n");

    } else {

    print ("Total number of matches: $matchcount\n");

    }

    #perl current_program.pl file1 file2

    #Enter the search pattern:

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    5/10

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    6/10

    $input[$count] =~ s/^[ \t]+//;

    $input[$count] =~ s/[ \t]+\n$/\n/;

    $input[$count] =~ s/[ \t]+/ /g;

    $count++;

    }

    print ("Formatted text:\n");

    print (@input);

    #This is a line of input.

    # Here is another line.

    #This is my last line of input.

    #^D

    #Formatted text:

    #This is a line of input.

    #Here is another line.

    #This is my last line of input.

    ##############################################################################

    #####

    ######## ### Pattern Matching ###

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    7/10

    ## Find the word "Perl"

    $pattern = 'Perl';

    ## Find "Perl" at the beginning of a line

    $pattern = '^Perl';

    ## Find sentences that contain an "i"

    $pattern = 'i';

    ## Find words starting in "i", i.e. a space preceeds the letter

    $pattern = '\si';

    ## Find strings containing a digit

    $pattern = '\d';

    ## Search for a digit followed by some stuff

    $pattern = '\d+.+';

    ## Find strings with a digit at the end of a line

    $pattern = '\d+$';

    ## Search for a digit, possible stuff in between, and another digit

    $pattern = '\d.*\d';

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    8/10

    ## Find four-letter words, i.e. four characters offset by word boundaries

    $pattern = '\b\w{4}\b';

    ## Sentences with three words, three word fields separated by white space

    $pattern = '\w+\s+\w+\s+\w+';

    ## Find sentences with two "e" letters, and possible stuff between

    $pattern = 'e.*e';

    ##############################################################################

    #####

    ############ Marking Regular Expression Sub-strings and Using Substitution

    ## Find five-letter words and replace with "Amazing"

    $pattern = '\b\w{5}\b';

    foreach(@strings)

    {s/$pattern/Amazing/;

    }

    ## Search for two digits in same line, and switch their positions

    foreach(@strings)

    { $pattern = '(\d)(.*)(\d)';

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    9/10

    if (/$pattern/)

    { print "Grabbed pattern: $pattern \$1 = $1 \$2 = $2 \$3 = $3\n";

    s/$pattern/$3$2$1/;

    }

    }

    ##############################################################################

    #####

    ########

    ##############################################################################

    #####

    ########

    # change all occurances of a string in a file to another string

    #

    if ($#ARGV != 3) {

    print "usage: chstring oldfile newfile oldstring newstring\n";

    exit;

    }

    $oldfile = $ARGV[0];

    $newfile = $ARGV[1];

    $old = $ARGV[2];

    $new = $ARGV[3];

  • 8/12/2019 WINSEM2013-14_CP3057_28-Jan-2014_RM01_demopattern1 (1)

    10/10

    open(OF, $oldfile);

    open(NF, ">$newfile");

    # read in each line of the file

    while ($line = ) {

    $line =~ s/$old/$new/;

    print NF $line;

    }

    close(OF);

    close(NF);

    ##############################################################################

    #####

    ########