kitty has asked for the wisdom of the Perl Monks concerning the following question:

I have an input file of the form :
MML v2.0; import defaultunit; unit conversion on; math top { t.min = 0; t.max = 3600; t.delta = 5; .........
I need to extract the t.min and t.max values. The code i have written does not print the value but prints t.min and t.max a 1000 times.
#!usr/bin/perl use strict; use warnings; use Getopt::Long; my $file; GetOptions("f|file=s" => \${file}) or die "invalid option\n"; if ($file) { my $fname = $file; open (INFO,"<",$fname) or die " Could not open\n" ; open (OUTFO,">>","text.txt") or die "Could not open\n"; my (@line, $myline); @line=<INFO>; foreach $myline(@line) { if($myline=~ /(t\.min=)(.*)(;)/) {print OUTFO "tmin = $2\n";}; if($myline=~ /(t\.max=)(.*)(;)/) {print OUTFO "tmax = $2\n";}; } close OUTFO; close INFO; }
Please help. Thankyou for all the help. I have to parse the whole file and print to a text file, i have gotten stuck in the first step itself, am kind of worried now. Thanks again.

Replies are listed 'Best First'.
Re: Help : Regular expression
by helphand (Pilgrim) on Feb 27, 2006 at 05:23 UTC

    Your patterns don't match the data. Try adding a space between the 'n' and '=' in (t\.min=) and between 'x' and '=' in (t\.max=).

    Scott

Re: Help : Regular expression
by Praveen (Friar) on Feb 27, 2006 at 05:21 UTC
    Try This
    while(<DATA>) { print "$1\t$2\n",if($_ =~ /(t\.min|t\.max)\s*=\s*(.*?);/); } __END__ MML v2.0; import defaultunit; unit conversion on; math top { t.min = 0; t.max = 3600; t.delta = 5;