> Perlwannabe wrote:
>>
>> I posted a problem to the mailing list with a similar question I had
>> some time ago regarding replacing data between fields.  Unfortunately,
>> I am having a difficult time with the problem.  I am including the
>> code and a sample of the data.  Could someone please give me a clue as
>> to why this is not working.  I have exhausted myself before
>> re-contacting the list.  When I run the code, the file remains exactly
>> the same and nothing is changed or
>> replaced.
>>
>> ########################   BEGIN CODE  ##########################
>>  #!/usr/bin/perl
>> use warnings;
>> use strict;
>> use LWP::Simple;
>>
>> my $file10 = "C:/Test_Folder/test_data/input.txt";
>> #declare the input
>> file as the static file name
>> open(FILE,"<$file10") || die "Could not open file for reading!
>> $!";                #open file for reading
>> open(TEMP,">$file10.tmp") || die "Could not open file for writing!
>> $!";                #open file for writing
>> while(<FILE>){                   #begin while
>>         s{ (?<=HOW) (.+?) (?=NUMBER:) } { ( $a = $1 ) =~ tr/\t/ /; $a
>>  }ex;
>>         print TEMP $_;                #print result to temp file
>> }                        #end while
>> #Close the files.  This should happen automatically, but the routine
>> is written for safety
>> close FILE || die "Could not close file! $!";
>> close TEMP || die "Could not close file! $!";
>> unlink $file10;
>> #remove the old file
>> rename("$file10.tmp",$file10) || die "The file could not be renamed!
>> $!";                #rename the temp file to the old file's name
>>
>> #####################  END CODE #####################################
>>
>> Here is a sample of the data file:
>>
>> Name:<tab>John Smith<tab>Address: 1234 Sparrow Ave.<tab>HOW MANY
>> CHILDREN: 3<tab>NUMBER OF PETS: 2<tab>TYPE OF PETS: Fish,
>> Dog<tab>FAVORITE NUMBER: 13<tab>City: New York<tab>State: New
>> York<tab>Zip: 11011
>>
>> Obviously, I do not want all of the useless information between
>> Address and City.  I want to delete everything between HOW and NUMBER:
>> (including the HOW and NUMBER:<tab>)  So my file will look like this:
>>
>> Name:<tab>John Smith<tab>Address: 1234 Sparrow Ave.<tab>City: New
>> York<tab>State: New York<tab>Zip: 11011
>
>
> According to your data, this seems to work:
>
> while ( <FILE> ) {
>     print TEMP join '',
>         grep /(?:^|\t)(?i:name|address|city|state|zip):/,  # fields to
> keep /(?:^|\t)[^:]+?:\s+[^\t]+/g;                       # split
> the fields
> }

I originally tried to do this, but it won't work.  The data doesn't
_always_ have a <tab> before address, sometimes (although seldom) it has a
<space>.  With grep I would miss the entire address.  However, if I were
to just select everything and replace it with nothing, that would be the
answer.  BTW...your solution above is right on the money for about 98% of
the file.  However, by simply scanning the line and deleting everything
between between HOW and NUMBER: that would give me 100%.  Any ideas?

BTW...can you see why the original code I posted is not working?






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to