I need some advice with working with a fixed length file that uses two different formats, the format switches every other line in the file. Here is an example of the file contents

03002068454210482 000000004204.572011-04-14 + 19:53:41I NTERNET C 750467375 ^M 0214833 + G02042954 ^M 03002068703214833 000000002558.662011-04-15 + 08:17:19I NTERNET C 761212737 ^M 0211561 + 05601207284 ^M 03002068802911561 000000001463.702011-04-15 + 08:40:52I NTERNET C 719807216 ^M 029911 + 00100275296 ^M
The lines that have "03" as the first two characters match the following patterns: 02:10:33:15:19:10:3:18:6:4 While the other lines match this pattern: 02:98:11:9 Is using read and unpack the best way to approach this? The way that I am currently working with this I don't think this will work here becuase it loads in a file that has the pattern that matches the currently loaded file, in all I am working with four files and this is the only one that differs like this.
#!/usr/bin/perl use strict; use warnigns; my $filename = 'fixedfile.txt'; my $datname = $filename; $datname =~ s/\.txt//g; my $fc = 0; my @fla; my @fna; open my $dat, '<', "$datname.dat"; while (<$dat>) { chomp; my @fields = split(/\|/); foreach(@fields) { my ($field, $length) = split(/\:/, $_); if ( $length < 1 ) { $length = 1; } $fla[$fc] = $length; $fna[$fc] = $field; $fc++; } } close $dat; open my $fixedfile, '<', "$filename"; while (<$fixedfile>) { chomp; s/\r|\n//g; s/^M//g; s/^\s*//; s/\s*$//; my $line = $_; my $dc = 0; my $start = 0; foreach (@fna) { my $garbage = substr($line,$start,$fla[$dc]); $garbage =~ s/\'//g; $garbage =~ s/\"//g; $garbage =~ s/\\//g; $garbage =~ s/\(//g; $garbage =~ s/\)//g; $garbage =~ s/^\s*//; $garbage =~ s/\s*$//; $start = $start + $fla[$dc]; $dc++; } close $fixedfile; }


In reply to Working with fixed length files by vendion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.