> -----Original Message-----
> From: Kevin Old [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, January 30, 2003 12:18 PM
> To: [EMAIL PROTECTED]
> Subject: Parsing Fixed Length data
> 
> 
> Hello all,
> 
> I have run into a situation that I've never faced before.  I've always
> parsed delimited (comma, pipe, etc) data.  Now I'm faced with parsing
> fixed-width data.  That's fine and I understand how to do it and have
> even found a module for it (Parse::FixedLength).
> 
> Say I have the following string and the first 10 characters are the
> first name, the second 10 are the last name and the next 7 are the
> price.
> 
>      Kevin        Old001.000
> 
> I understand that with the name fields I can just strip off the \s
> characters and then I have my first name, but what about the price? 
> Some times it could be like 1000.00 or 0100.00....but with 
> the preceding
> zeros it makes it confusing.  Do I need to explicitly strip off the
> preceding zeros?  
> 
> Any help on dealing with fixed width data is appreciated,
> 
> Kevin
> -- 



perldoc -f unpack

<snip>
use strict;
use warnings;

my %records;

my $surname;
my $given_name;
my $price;

my @fields;

my $i=0;
while (<DATA>)
{
        @fields = unpack("A10A10A7", $_);
        
        $records{$i} = { 
                given_name => $fields[0],
                surname    => $fields[1],
                price      => $fields[2]
        };

        $i++;
}

print "$records{0}{surname}\n";
print "$records{0}{given_name}\n";
print "$records{0}{price}\n";

print "\n";

print "$records{1}{surname}\n";
print "$records{1}{given_name}\n";
print "$records{1}{price}\n";


__DATA__
Kevin     Old       001.000
Someone   Else        1.000
</snip>

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

Reply via email to