Help with pattern matching
From: A Lukaszewski (al_at_onetel.net.uk)
Date: 04/01/04
- Next message: Mike Blezien: "Re: Check if another script running"
- Previous message: Jayakumar Rajagopal: "RE: Check if another script running"
- Next in thread: John W. Krahn: "Re: Help with pattern matching"
- Reply: John W. Krahn: "Re: Help with pattern matching"
- Reply: R. Joseph Newton: "Re: Help with pattern matching"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 01 Apr 2004 15:25:20 +0000 To: beginners@perl.org
Greetings all,
I have a comma-delimited lexical file with four fields: line number, the
first part of a word, the second part of a word, and the word combined.
The first and fourth fields are only for reference. The program I am
developing is very simple. If field two and field three both have
accents in them, then print the line to an output file.
The heavily-commented program is below. Thus far, all I get is an exact
replica of the input file. In addition to a plain binding operator of
'=~ //', I have also tried explicit matching (m//) and regex (qr//).
#!/usr/bin/perl
#############################################################
#############################################################
# A PROGRAM TO READ THE SUB-WORD HEADERS OF A #
# COMMA-DELIMITED FILE #
# AND DETERMINE WHICH LINES HAVE MULTIPLE ACCENTS #
#############################################################
#############################################################
use strict;
###################################
# OPEN THE INPUT AND OUTPUT FILES #
###################################
my ($file, $outfile);
$file = 'y.csv' ;
# Name the input file
$outfile = 'y.res';
# Name the output file
open(INFO, "$file" ) or die "Cannot open $file:$!\n";
# Open the input file or report failure
open(OUT, ">>$outfile") or die "Cannot open file y.res!\n";
# Open the output file
########################################
# INITIALIZATION OF SCALARS AND ARRAYS #
########################################
my $line; # = scalar by which program steps through data
my $fieldEval1; # = holding scalar for evaluating whether the
# first half of the word has an accent in it
my $fieldEval2; # = holding scalar for evaluating whether the
# second half of the word has an accent in it
my @field; # = holding array for the split line
#######################################################
# FOREACH CONTROL TO READ THE INPUT FILE LINE BY LINE #
# AND MANIPULATE THE DESIRED DATA TO AN OUTPUT FILE #
#######################################################
foreach $line (<INFO>) {
# Assign the contents of the input file to $line one line at time for
# evaluation.
chomp ($line); # remove input field separator
next unless $line; # skip blank lines
@field = split /,/, $line; # Read each line as four fields split by
commas
# Assign the second field to an evaluation scalar
$fieldEval1 = $field[1];
# Assign the third field to an evaluation scalar
$fieldEval2 = $field[2];
# Test whether BOTH the second or third fields have accents in them
# Accents are represented by the following characters: k K c ; ' [ { ] }
# \ and |.
if ({$field[1] =~ /[kKc;'\[\{\]\}\\\|]/} && {$field[2] =~
/[kKc;\'\[\{\]\}\\\|]/}) {
print OUT "$line\n"; # If so, print the line to file
}
}
close (OUT); # Close the output file
close(INFO) ; # Close the input file
__END__
Any helpful comments would be very much appreciated.
Best,
Al
Al Lukaszewski
St Andrews, Scotland
- Next message: Mike Blezien: "Re: Check if another script running"
- Previous message: Jayakumar Rajagopal: "RE: Check if another script running"
- Next in thread: John W. Krahn: "Re: Help with pattern matching"
- Reply: John W. Krahn: "Re: Help with pattern matching"
- Reply: R. Joseph Newton: "Re: Help with pattern matching"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|