For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > problem with simple pattern matching









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author problem with simple pattern matching
Jim

2007-08-29, 7:59 am

Hi,

I hope this is simple, as i am very new to perl=2E

I am trying to use pattern matchinig to create many srcipts from
a template file which i have read into a list then print out to files=2E

the problem is the patter match only matches on the first 2 successful mat=
ches
after that it keeps the valuse found in those first 2 successes and writes=
them out
after that

the sub below is called in a for loop which pulls out some new data to fil=
l the args

Thanks

Jim

the lines im trying to match are

#! DATASET=3D" C:\jim\MinePlan\StratMinePlan\Data_In\20
07\2 year detailed s=
tage plan\DGN_IN=2Edgn"
DEFAULT_MACRO SourceDataset_IGDS_1 C:\jim\MinePlan\StratMinePlan\Data_In\2=

007\2 year detailed stage plan\DGN_IN=2Edgn
#! DATASET=3D" C:\jim\MinePlan\StratMinePlan\Data_In\20
07\2 year detailed s=
tage plan\CSVOUT"
DEFAULT_MACRO DestDataset_CSV_1 C:\jim\MinePlan\StratMinePlan\Data_In\20
07=
\2 year detailed stage plan\CSVOUT
#! DATASET=3D" C:\jim\MinePlan\StratMinePlan\Data_In\20
07\2 year detailed s=
tage plan\DESTDIR"
DEFAULT_MACRO DestDataset_MAPINFO_1 C:\jim\MinePlan\StratMinePlan\Data_In\=
2007\2 year detailed stage plan\DESTDIR

as i said the first file out is written with the bits filled in correctly =
=2E=2E=2E but only the first :(

sub mk_fme_from_template
{

my $data_class =3D shift;
my $dgn_fname =3D shift;
my $prediction_year =3D shift;
my $dgn_src_dir =3D shift;
my $mapinfo_dest_dir =3D shift;
my $mapinfo_text_dir =3D shift;
my $i =3D 0;

# search patterns
my $search_dgn_in =3D "DGN_IN";
my $search_dest_dir =3D "DESTDIR";
my $search_csv_out =3D "CSVOUT";

my $prepend;
my $curr_yr_pred =3D $current_year =2E "_" =2E $data_class =2E "_" =2E $=
prediction_year;

foreach my $line (@fme_template_file)
{

if ($line =3D~ m/$search_dgn_in/g)
{
if ($line =3D~ m/DEFAULT_MACRO/g)
{
$prepend =3D "DEFAULT_MACRO SourceDataset_IGDS_1 ";
} else {
$prepend =3D "#! DATASET=3D\"";
}
$line =3D "$prepend$dgn_src_dir\\$dgn_fname\n";
$dgn_in =3D 1;
}

if ($line =3D~ m/$search_dest_dir/)
{
if ($line =3D~ m/DATASET/)
{
$prepend =3D "#! DATASET=3D\"";
} else {
$prepend =3D "DEFAULT_MACRO DestDataset_MAPINFO ";
}
$line =3D $prepend =2E $mapinfo_dest_dir =2E "\"\n";
}

if ($line =3D~ m/$search_csv_out/)
{
if ($line =3D~ m/DATASET/)
{
$prepend =3D "#! DATASET=3D\"";
} else {
$prepend =3D "DEFAULT_MACRO DestDataset_CSV_1 ";
}
$line =3D $prepend =2E $mapinfo_text_dir =2E "\\" =2E $prediction_ye=
ar =2E "=2Ecsv\"\n";
}

$fme_output_script[$i++] =3D $line;
}

write_fme_file($data_class, $prediction_year);
}

Chas Owens

2007-08-29, 9:59 pm

On 8/29/07, Jim <jim_n@tpg.com.au> wrote:
> Hi,
>
> I hope this is simple, as i am very new to perl.
>
> I am trying to use pattern matchinig to create many srcipts from
> a template file which i have read into a list then print out to files.
>
> the problem is the patter match only matches on the first 2 successful matches
> after that it keeps the valuse found in those first 2 successes and writes them out
> after that
>
> the sub below is called in a for loop which pulls out some new data to fill the args

snip

I think your example is missing information we need to help you.
Please create a small, but complete, script that demonstrates your
problem along with input and expected output.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com