#!/usr/bin/perl -w # turn on strict forcing definition of variables and subs/functions use strict; # # Declare Function/Sub Routines sub Searchme; #returns -1 if substring not found # used with @Search_Array defined later. sub Removechar; #returns original line with character removed. # Two parameters are passed, the line and the character to remove. # We are using this to remove doublequotes from the input line. sub Checknotspacesonly; #returns emptystring from input line, if line # is only spaces. The only parameter passed is the input line. # Declare Variables my @Search_Array = ("email", "website"); # This is what will be searched for. A line # that matches any of the substrings contained in the # Search_Array will be filetered out. my $infile = "" ; # input file my $outfile = "" ; # output file my $line = "" ; # line of input file my $len = 0; # Used for getting length of a string. my $getresult = -1; # -1 will denote not found after searching for # a substring within a string. #Sub Routine / Function Section sub Searchme # Returns -1 if Substring not found in input line # Otherwise it returns a number of zero or greater. { my $j = 0; #iteration loop scalar counter my $result = -1; # used for checking for substring # A "-1" denotes not found. # Given an input line search for elements in the Search_array. my ($inline, @Search2_array) = @_; my $knum = 0; #Set and define default number of Substrings to search. $knum = scalar@Search2_array; #Get number of elements (substrings) # to search line for. # Note we must subtract -1 from $knum so we do not go past the # end of the search array. for ( $j = 0; $j <= ($knum -1 ); $j++) { $result = index($inline, $Search2_array[$j]); # A value of -1 is returned for not found. if ($result >= 0) {return $result; next;} # Exit prematurely found a substring # that matches. } return $result; # if we get here no matching substrings. } # End sub Searchme sub Removechar # Returns the line with the character removed. # The input line and the character to remove is passed in as a # character string. { my ($inline, $chartoremove) = @_; $inline =~ s/$chartoremove//g; return $inline; } #end sub Removechar sub Checknotspacesonly # A line is passed in and its length is checked. # A string equal to spaces only of this length is created. If the # strings matched, the line is returned as null string to be ignored, # that is treated later as blank. { my ($inline) = @_; my $len = 0; # assume blank string of zero length. my $spacestr = ""; # assume blank string my $j =0; # iteration loop scalar counter. $len = length($inline); # Use concatenation operator to create string of desired length # containing only spaces. for ($j =0; $j <= ($len -1);$j++) { $spacestr = $spacestr. " "; } if ($inline eq $spacestr) { $inline = ""; # set line returned to null string for all spaces } return $inline; } # End sub Checknotspacesonly # End Subroutine Function Section # # Begin Main Program Section ################################### # # Usage: perlparselabel input_filename output_filename # input_file_name will be the name of the input file # to process. output_file_name will be the output file. # # This program will take an input file and eliminate the lines that # contain blanks, the word "email" or the word "website". This is so # that a mailing list can be generated for US Postal mail. This is a # simplified version of a program used in real life. The output # file name will be whatever you chose to call it. # i.e. ./perlparselabel infile.txt outfile.txt $infile = shift(@ARGV); # Shift arguement call array to left to getinput file open(INFILE,$infile) || die "Couldn't open input file $! \n"; $outfile = shift(@ARGV); # Shift arguement call array to left to get output file open (OUTFILE, ">$outfile") or die "$! error trying to overwrite"; while() # Keep Reading file to end { # Good practice to store $_ value because # subsequent operations may change it. $line = $_; # get input line from reading file. # Good practice to always strip the trailing # newline from the line. chomp($line); $getresult = Searchme($line, @Search_Array); if ($getresult >= 0 ) {next;} # leave while loop do not write this line. # Remove the doublequote using function created. $line = Removechar ($line, '"' ); $line = Checknotspacesonly ($line); $len = length($line); if ($len > 0) { # check not blank line print OUTFILE " $line \n"; } #end of while loop block } close(INFILE); close(OUTFILE); # End Main Section ************************** exit;