PMReader has asked for the wisdom of the Perl Monks concerning the following question:
I had a quick-and-dirty script that worked with the substring function as long as the offset and length of text were the same. This is no longer true. Now the length of text to be extracted is variable (and sometimes contains spaces).
I can determine the text around the substring. Let's say start and end.
I thought I could more generically extract the substring with split /start($_)end/ but I can't get the syntax right and it won't compile.
Here's the original script. It works, finding one $transactID and a dozen or so item/quantity instances in each file, but it's inflexible:
#!/usr/bin/perl -w use strict; use warnings; my $transactID = "item=" ; #### Usually a number my $itemname = "itemname" ; #### Usually alphanumeric my $quantity = "qty" ; #### Always numeric open(IN,'ItemsFile.rtf') or die("can't open input file\n"); open(OUT,'>results.txt') or die("can't open output file\n"); while(<IN>){ print OUT "0 ", substr($',3,17), "; " if($_ =~/\b$transactID\b/i); print OUT "1 ", substr($',2,12), "; " if($_ =~/\b$itemID\b/i); print OUT "2 ", substr($',13,3), "; " if($_ =~/\b$quantity\b/i); + } close(IN); close(OUT);
Note that start sometimes contains spaces.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extracting variable-length strings between delimiters
by 7stud (Deacon) on Feb 12, 2010 at 04:21 UTC | |
|
Re: Extracting variable-length strings between delimiters
by Anonymous Monk on Feb 12, 2010 at 00:54 UTC | |
|
Re: Extracting variable-length strings between delimiters
by ahmad (Hermit) on Feb 12, 2010 at 02:04 UTC | |
by PMReader (Initiate) on Feb 12, 2010 at 06:31 UTC | |
by ahmad (Hermit) on Feb 12, 2010 at 08:39 UTC |