in reply to Re: Splitting file into separate files based on record lengths and identifiers
in thread Splitting file into separate files based on record lengths and identifiers

Morning all!

While I massively appreciate the scripts here I just feel like I'm not learning anything, which is not good for anybody :) I have in my head the logic (probably flawed at this stage) I want to use and I've started from scratch on something really simple so perhaps people could help me go down that path?

#!/usr/bin/perl use strict; use warnings; my $string = '0004$ADAM0002*330005LTESTL0005STESTS0005JTESTJ0005ZTESTZ +'; my $len1 = substr $string, 0, 4; my $type = substr $string, $len1, 1; my $fragment = substr $string, $len1+1, $len1; my $nextpos = $len1+5; print " string: $string\n"; print " 1st data length: $len1\n"; print " data: $fragment\n"; print " type: $type\n"; print " next record lenth pos: $nextpos\n";
string: 0004$ADAM0002*330005LTESTL0005STESTS0005JTESTJ0005ZTESTZ 1st data length: 0004 data: ADAM type: $ next record lenth pos: 9

The way I'd like to approach this is to start at beginning of the line and use the length string in each case to extract the string. So in the example above I use the '0004' to identify the data in the first field and then to identify where the next length marker for record 2 begins

What I'm stuck with is putting this into a loop so that it'll continue down the string, using each record length field as it goes, to strip out the data

Thanks!

Replies are listed 'Best First'.
Re^3: Splitting file into separate files based on record lengths and identifiers
by monty77 (Initiate) on Aug 27, 2010 at 17:49 UTC

    Update .. got this far, but it just spits out the same data over and over again so I've not got the while loop quite right, any ideas?

    #!/usr/bin/perl use strict; use warnings; my $string = '0004$ADAM0002*330005LTESTL0005STESTS0005JTESTJ0005ZTESTZ +'; my $currentpos = 0; while ($string) { my $recordlength = (substr $string, $currentpos, 4)+5; my $recordtype = substr $string, $currentpos+4, 1; my $fragment = substr $string, $currentpos+5, $recordlength-5; my $nextstartpos = $recordlength+1; print " string: $string\n"; print " record type: $recordtype\n"; print " data: $fragment\n"; print " tot record length: $recordlength\n"; print " next start pos: $nextstartpos\n"; my $currentpos = $currentpos + $recordlength+1 }

    Thanks!

Re^3: Splitting file into separate files based on record lengths and identifiers
by Marshall (Canon) on Aug 27, 2010 at 19:05 UTC
    #!/usr/bin/perl -w use strict; use Data::Dumper; my @input=( '0010$ADAM SMITH0003*3330004%19770004$BOB 0001*40004%1967', '0004$ADAM0002*330005LTESTL0005STESTS0005JTESTJ0005ZTESTZ' ); foreach my $data (@input) { print "\nSTARTING NEW LINE ......\ndata = $data\n"; while ($data) { print "\n"; my $len = substr($data,0,4); my $type = substr($data,4,1); my $stuff= substr($data,5,$len); substr($data,0,$len+5,''); #delete this "set of data" print "data length = $len\n"; print "data = $stuff\n"; print "type = $type\n"; } } __END__ STARTING NEW LINE ...... data = 0010$ADAM SMITH0003*3330004%19770004$BOB 0001*40004%1967 data length = 0010 data = ADAM SMITH type = $ data length = 0003 data = 333 type = * data length = 0004 data = 1977 type = % data length = 0004 data = BOB type = $ data length = 0001 data = 4 type = * data length = 0004 data = 1967 type = % STARTING NEW LINE ...... data = 0004$ADAM0002*330005LTESTL0005STESTS0005JTESTJ0005ZTESTZ data length = 0004 data = ADAM type = $ data length = 0002 data = 33 type = * data length = 0005 data = TESTL type = L data length = 0005 data = TESTS type = S data length = 0005 data = TESTJ type = J data length = 0005 data = TESTZ type = Z