On Dec 11, 2003, at 12:27 PM, Larry Sandwick wrote:
With the little amount of Perl that I know, I have come to a dilemma. I do not know how to parse this file so in column 1 (24165) is the number duplicate for every item number in the list. I also need in the 2 column (O185850) to duplicate itself for every item. The 3rd item (05/30/03)needs to follow the same process. The file format is below, before the parser.
I know I can split the file on "|" but because the data is not
consistent and my skill set is limiting me to re-parse this file into a
file I can upload into MySql I am asking for help and suggestions I do I
detect that column 1 has change ? Some how I need to read in the first
three lines for every backorder, before I can start outputting the data?
.
personally I say Shoot the "person" who came up with the file lay out - but what you need is a bit of peek ahead coding.
my $entry; # our entry - from the first line my $key = ''; # the 'key' - that bit from the second line my $date = ''; # date - that occurs on the third line my $line; # what we will cache the 'rest of the date line' # I was using Inline::Files - so let us assume that # you opened the file as INFO1 while ( my $stuff = <INFO1> ) { chomp($stuff); next if ($stuff =~/^\s*$/); # blow off empty lines # thin down the spaces around the "|" # this way the $user in particular will be 'empty' # as well as streamline the other fields
$stuff =~ s/\s*\|\s*/|/g; my ($user,$itemkey,$tail) = split(/\|/, $stuff, 3);
if ( $user ) { $entry = $user; # save the entry info $key = $date = ''; # reset any previous key and date } else { if ( $key && $date ) # if we have both, this is a follow on item { print "$entry|$key|$date|$tail\n"; } elsif ( $key ) # if we have the key we need the date { $date = $itemkey; # collect the date # send out foist the Old Line and then our new one print "$entry|$key|$date|$line\n"; print "$entry|$key|$date|$tail\n"; } else { $key = $itemkey; # save the key $line = $tail; # save the tail } } # else side of have user value }
that will generate your 24165|O18580|05/30/03|259|LEATHER BOOK SIDE TABLE|1|1|295.00 24165|O18580|05/30/03|1774|FUNCTIONAL TABLE LAMP|1|0|35.00 24165|O18580|05/30/03|1773|FUNCTIONAL FLOOR LAMP|1|0|62.50 24165|O18580|05/30/03|1302|MOROCCAN FLORAL BX,BRASS|1|0|29.00 24165|O18580|05/30/03|1666|CUBA COFFEE TABLE|1|1|290.00 24165|O18580|05/30/03|1666|CUBA SIDE TABLE|1|1|147.50 24310|O18813|07/29/03|1145|FLEUR-DE-LIS DOCUMENT BOX|1|0|52.50 24310|O18813|07/29/03|1549|TAOS CENTERPIECE|1|1|65.00 24310|O18813|07/29/03|1729L|FRENCH BOX BOOKEND, LEFT|1|1|69.00 24310|O18813|07/29/03|1729R|FRENCH BOX BOOKEND, RIGHT|1|1|69.00 24310|O18813|07/29/03|1549|WINDEMERE BOOK CADDY|1|0|85.00 24310|O18813|07/29/03|1774|REVOLVING BOOK TABLE|1|1|234.00 24310|O18813|07/29/03|1574|NORMANDY 3 DRAWER CONSOLE|1|1|330.00 24310|O18813|07/29/03|1765|LEATHER BOOKS,ASST.SET/12|1|1|175.00 5522|O18549|05/20/03|1551|HEX LEATHER GARDEN STOOL|1|0|130.50 5522|O18549|05/20/03|1749|TRIVET STAND|1|1|87.75 5522|O18549|05/20/03|1801|DESK BOX, VICTORIA|1|1|85.50 5522|O18549|05/20/03|1549|TAOS CENTERPIECE|1|1|58.50
Still think you should shoot who EVER came up with the original file format...
HTH....
ciao drieux
---
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>