in reply to Splitting file into separate files based on record lengths and identifiers
Hope this will help you. Uses Hash.
#!/usr/bin/perl -w # use warnings; use strict; my %hash = (); while (<DATA>) { chomp($_); my $n = length($_); my $i = 0; while ($i<$n) { my $long = substr($_,$i,4); $i += 4; my $delim = substr($_,$i,1); $i += 1; my $val = substr($_,$i,$long); $i += $long; # print $long,' ',$delim, ' ', $val,"\n"; $hash{ $delim } .= $val.','; } } s/,\z// for values %hash; while ( my ($key, $value) = each(%hash) ) { print "$key => $value\n"; } print "size of hash: " . keys( %hash ) . ".\n"; print '-' x (60),"\n"; __DATA__ 0004$ADAM0002*330004%19770004$BOB 0002*430004%1967 0003$XDA0002*440004%22220003$XOB0002*990004%3333
Results.
Process started >>> $ => ADAM,BOB ,XDA,XOB % => 1977,1967,2222,3333 * => 33,43,44,99 size of hash: 3. ------------------------------------------------------------ <<< Process finished.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Splitting file into separate files based on record lengths and identifiers
by monty77 (Initiate) on Aug 26, 2010 at 21:53 UTC | |
by Generoso (Prior) on Aug 27, 2010 at 17:35 UTC |
In Section
Seekers of Perl Wisdom