English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm trying to make a search in PERL that searches for the occurrence of a periond, question mark, and comma.

Heres what I have so far (this just searches for 0 to 1 ?s)

open(AFILE, $ARGV[0]);

if ( =~ /\??/){
print ;
} else {
print 'no match';
}

If someone also has some time, can they explain to me why / \. / doesn't search the whole textfile for a period. Is it because I don't have a *,?, or + after the period i.e / \.+/

2007-03-22 05:50:06 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

You don't need a loop to suck down a file!

my @records = ;

my $data = join('',@records);

now you have the entire file in a string.

/\??/ matches 0 to 1 question marks. (a weird pattern, everything has 0 to 1 question marks!)

my $count = $data =~ /\?/;

gives you the number of question marks.

2007-03-22 07:50:52 · answer #1 · answered by jake cigar™ is retired 7 · 1 0

The angle operator () reads only the next line from the file. The usual practice is to use a while loop to read in the whole file:
while(){
$string .= $_;
}
if($string =~ /\.\?,/){
print $string;
}else{
print "no match";
}

2007-03-22 07:10:12 · answer #2 · answered by injanier 7 · 0 1

fedest.com, questions and answers