mantra2006 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I am looking for a way to read a flat file and get the necessary data from it.
for eg.
00UPDATE2006121509200120061215095000000000058Version 02.7.0.0

The above is one line and the script has to read line and get 00, UPDATE, 2006121509, etc as data...

Appreciate help in suggesting the best way to do this...
Thanks & Regards
Sridhar

Replies are listed 'Best First'.
Re: Reading a Flat File and Extracting Data
by davorg (Chancellor) on Jan 18, 2007 at 15:14 UTC

    If (as it appears) your data is in fixed width format, then you'll almost certainly find unpack to be useful - along with the "pack" tutorial.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Reading a Flat File and Extracting Data
by ferreira (Chaplain) on Jan 18, 2007 at 15:11 UTC
Re: Reading a Flat File and Extracting Data
by BrowserUk (Patriarch) on Jan 18, 2007 at 15:18 UTC

    Use the simplest tool available unless you know you have a reason for reaching for the big guns. I've had to guess where the fields are:

    c:\>p1 Perl> $line = '00UPDATE2006121509200120061215095000000000058Version 02 +.7.0.0';; Perl> @fields = unpack 'A2 A6 A10 A4 A10 A13 A*', $line;; Perl> print for @fields;; 00 UPDATE 2006121509 2001 2006121509 5000000000058 Version 02.7.0.0

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Reading a Flat File and Extracting Data
by Moron (Curate) on Jan 18, 2007 at 15:50 UTC
    Provided the fields really are in fixed positions, the unpack idea given by BrowserUK looks very apt. If that isn't the case (for example LONGERNAME can be instead of UPDATE) , I'd make a sequence of regexp rules to match on a what-comes-next basis and iterate on it, something like:
    my $fieldSpec = [ { name => "name1", pattern => '\d{2}' }, { name => "trtype", pattern => '\D+' }, { name => "timestamp", pattern => '\d{8}' }, # etc. ]; my $results = []; open my $fh, "<$file" or die $!; while( <$fh> ) { my $result = {}; for my $elt ( @$fieldspec ) { my $pat = $elt -> { pattern }; /^($pat)(.*)$/ or die "unexpected format $_ at $."; $result -> { name } = $elt -> { name }; $result -> { value } = $1; $_ = $2; } push @$results, $result; }
    ...producing results as a reference to an array of hash in this particular example

    -M

    Free your mind