in reply to reading in fixed width
substr: Note that second parameter is 0-based. A warning will be given if the start position is beyond (but not at!) the end of the string; the result will be shorter than the requested length if $in isn't long enough.
Beware! If you use substr in an lvalue context, the warning gets promoted to an error:$one = substr($in, 23, 15); $two = substr($in, 28, 10);
regex: use one (?=) anchored at the beginning per field. The offsets are still 0-based.$ perl -we'sub foo { print $_[0] } eval { foo(substr "abc", 4, 1); 1} +or die "croak: $@"' croak: substr outside of string at -e line 1.
If columns may be shorter, use .{0,15} and .{0,10} or similar. If a starting column is beyond the end of the string, the regex will fail.$in =~ /^ (?=.{23}(.{15})) # field one (?=.{28}(.{10})) # field two /xs or warn "bad input: $in ";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: reading in fixed width
by holli (Abbot) on Feb 27, 2005 at 21:17 UTC |