in reply to Google has failed me! Using pack and substr for fixed width file output
Following your pattern, substr($line,190,507) looks like it should be substr($line,190,317).
For what it's worth, I deal with flat files quite a bit. Generally, though, I use pack and unpack instead of substr. That way, I can use the same format string for both packing and unpacking, kinda like this:
my $packfmt = "A135A11A6A6A20A8A3A978"; my $outline = pack $packfmt, " ", "DATE=${mdy}", ${mdy}, ${hms}, " ", "G1ADP", $CLIENT_ID, " "; # Unpack, trailing blanks preserved (undef, $DMDY, $mdy, $hms, undef, $CLIENT_ID) = unpack $packfmt, $outline; # Unpack 1: remove trailing blanks (same format, but # using map to trim the strings): (undef, $DMDY, $mdy, $hms, undef, $CLIENT_ID) = map { s/\s+$//; $_ } unpack $packfmt, $outline; # Unpack 2: remove trailing blanks, different format my $parsefmt = lc($packfmt); (undef, $DMDY, $mdy, $hms, undef, $CLIENT_ID) = unpack $parsefmt, $outline;
Note: I don't have perl on my work machine, so this is (a) from memory, and (b) quite possibly a bit broken.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Google has failed me! Using pack and substr for fixed width file output
by Anonymous Monk on Apr 08, 2014 at 20:32 UTC | |
|
Re^2: Google has failed me! Using pack and substr for fixed width file output
by spudulike (Novice) on Apr 09, 2014 at 06:15 UTC | |
by AnomalousMonk (Archbishop) on Apr 09, 2014 at 09:13 UTC |