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

Hello. I wish to open a flatfile test.txt containing a single record and display this in a html table. Problem is that I only want to display certain fields and these fields, while always delimited with the same char are sometimes written on a newline. I can handle the nice one-record-per-row flatfile db's but this is a step beyond me. Any chance of a push in the right direction, it would be appreciated
Thanks!

***** BEGIN CONTENTS test.txt *******
SMQ2#TEST_2PASS TEST_671 TEST_ADV TEST_LGA TEST_ROOS#2#0
testguy#2002-07-24#13:47#testguy#2002-09-12#12:05#I295
Selective retest only
*#*
1
2 2 2 2 2 2 2 2
1# elapsedMinutes
elapsedMinutes#elapsedMinutesPass2
1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
**** RECORD EOF ***********


The fields I want to extract and display are..

Line 1
SMQ2 -> Batch Suffix ID
TEST_2PASS -> Associated Operations

Line 2
testguy -> Record Created
2002-07-24 -> Record Created Date
13:47 ->Record Created Time
testguy -> Record Last Updated
2002-09-12 -> Record Last Updated Date
12:05 -> Record Last Updated Time
I295 Selective retest only -> Record Description

Replies are listed 'Best First'.
Re: Difficult flatfile db record
by samurai (Monk) on Sep 16, 2002 at 16:03 UTC
    You could probably first just slurp lines one and two into an array:

    open FH, $file or die "Can't open $file: $!"; my @lines = (<FH>)[0,1]; close FH;
    If the fields are fixed length, you can separate them with unpack. If they are whitespace delimited, split /\s+/ will do to break up the line. Otherwise, you might need a hefty regex. But it looks like they're whitespace delimited, so you could do:

    my %data = (); # define all field names my @line1_fields = qw/ batch_suffix_id associated_operations /; my @lines2_fields = qw/ record_created record_created_date ... /; @data{ @line_1_fileds } = split /\s+/, $lines[0]; @data{ @line_2_fields } = split /\s+/, $lines[1];
    There, now you have a hash with all your field names.

    --
    perl: code of the samurai

      Thank's Samurai,

      I'll go play around with that. Fields in each line are # delimited so I should be okay..

      -testguy
        Oops... heh. Yeah, just split /#/ instead of /\s+/ :)

        --
        perl: code of the samurai