Help with pattern matching

From: A Lukaszewski (al_at_onetel.net.uk)
Date: 04/01/04


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



Relevant Pages

  • Re: Help is needed to compile C program using Visual Studie 2005
    ... the pdb file that was used when this precompiled header was created, ... an output file whose name has the following format: ... The length of input file paths and name must be less than 256; ... while(i < DefinedVariableArrayIndex) { ...
    (microsoft.public.vc.language)
  • Re: Need advice on File I/O
    ... open the input file and open an output file, ... you would still have the input file unchanged. ... On all currently supported operating systems, ...
    (comp.soft-sys.matlab)
  • Re: Help with pattern matching
    ... then print the line to an output file. ... > replica of the input file. ... In Perl, CamelBack is generall reserved for package names. ... where their meaning must be expressed in comments. ...
    (perl.beginners)
  • Re: Help with pattern matching
    ... then print the line to an output file. ... > replica of the input file. ... If you had had warnings enabled as well as strict you might have found ... > # Assign the second field to an evaluation scalar ...
    (perl.beginners)
  • Re: Difficult text file to parse.
    ... > records are which there are only two, look at the output file below to ... I want to show the delimiters even if ... > My sample Input file: ... [sample input and output files with long fields snipped] ...
    (comp.lang.perl.misc)