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?
>
> This is the file before ( see below what is should look like after the
> parser. (Before)
>
> 24165| DEF | | | | |
> |O18580 |259 |LEATHER BOOK SIDE TABLE | 1| 1| 295.00
> |05/30/03|1774 |FUNCTIONAL TABLE LAMP | 1| 0| 35.00
> | |1773 |FUNCTIONAL FLOOR LAMP | 1| 0| 62.50
> | |1302 |MOROCCAN FLORAL BX,BRASS | 1| 0| 29.00
> | |1666 |CUBA COFFEE TABLE | 1| 1| 290.00
> | |1666 |CUBA SIDE TABLE | 1| 1| 147.50
> 24310| ABC | | | | |
> |O18813 |1145 |FLEUR-DE-LIS DOCUMENT BOX | 1| 0| 52.50
> |07/29/03|1549 |TAOS CENTERPIECE | 1| 1| 65.00
> | |1729L |FRENCH BOX BOOKEND, LEFT | 1| 1| 69.00
> | |1729R |FRENCH BOX BOOKEND, RIGHT | 1| 1| 69.00
> | |1549 |WINDEMERE BOOK CADDY | 1| 0| 85.00
> | |1774 |REVOLVING BOOK TABLE | 1| 1| 234.00
> | |1574 |NORMANDY 3 DRAWER CONSOLE | 1| 1| 330.00
> | |1765 |LEATHER BOOKS,ASST.SET/12 | 1| 1| 175.00
> 5522 | XYZ | | | | |
> |O18549 |1551 |HEX LEATHER GARDEN STOOL | 1| 0| 130.50
> |05/20/03|1749 |TRIVET STAND | 1| 1| 87.75
> | |1801 |DESK BOX, VICTORIA | 1| 1| 85.50
> | |1549 |TAOS CENTERPIECE | 1| 1| 58.50
>
> (After)
>
> Format user | itemkey | date | item | description | order | backorder | cost
>
> 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|1801 |DESK BOX, VICTORIA | 1| 1| 85.50
>
> Any suggestions would greatly be appreciated !!!
This seems to do what you want:
#!/usr/bin/perl
use warnings;
use strict;
my ( %record, $user );
while ( <DATA> ) {
my @fields = split /\|/ or next;
if ( /^\d/ or eof DATA ) {
for my $key ( keys %record ) {
print join( '|', $key, @{ $record{ $key } }{ qw/ itemkey date / }, $_ )
for @{ $record{ $key }{ data } };
}
%record = ( $user = $fields[ 0 ] => {} );
}
else {
if ( $fields[ 1 ] =~ m|\d+/\d+/\d+| ) {
$record{ $user }{ date } = $fields[ 1 ];
}
elsif ( $fields[ 1 ] =~ m|\d+| ) {
$record{ $user }{ itemkey } = $fields[ 1 ];
}
push @{ $record{ $user }{ data } }, join '|', splice @fields, 2;
}
}
__DATA__
24165| DEF | | | | |
|O18580 |259 |LEATHER BOOK SIDE TABLE | 1| 1| 295.00
|05/30/03|1774 |FUNCTIONAL TABLE LAMP | 1| 0| 35.00
| |1773 |FUNCTIONAL FLOOR LAMP | 1| 0| 62.50
| |1302 |MOROCCAN FLORAL BX,BRASS | 1| 0| 29.00
| |1666 |CUBA COFFEE TABLE | 1| 1| 290.00
| |1666 |CUBA SIDE TABLE | 1| 1| 147.50
24310| ABC | | | | |
|O18813 |1145 |FLEUR-DE-LIS DOCUMENT BOX | 1| 0| 52.50
|07/29/03|1549 |TAOS CENTERPIECE | 1| 1| 65.00
| |1729L |FRENCH BOX BOOKEND, LEFT | 1| 1| 69.00
| |1729R |FRENCH BOX BOOKEND, RIGHT | 1| 1| 69.00
| |1549 |WINDEMERE BOOK CADDY | 1| 0| 85.00
| |1774 |REVOLVING BOOK TABLE | 1| 1| 234.00
| |1574 |NORMANDY 3 DRAWER CONSOLE | 1| 1| 330.00
| |1765 |LEATHER BOOKS,ASST.SET/12 | 1| 1| 175.00
5522 | XYZ | | | | |
|O18549 |1551 |HEX LEATHER GARDEN STOOL | 1| 0| 130.50
|05/20/03|1749 |TRIVET STAND | 1| 1| 87.75
| |1801 |DESK BOX, VICTORIA | 1| 1| 85.50
| |1549 |TAOS CENTERPIECE | 1| 1| 58.50
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>