Hello, everybody!
I've got a data-format (german banking, if anybody cares) that specifies a variable number (up to five) lines of 27 characters each. My upstream (norisbank) gives me this as a space, 27 characters, a space, 27 characters, etc. Well and good, and easy to split, right?
Not so much. My bank will also give me less then 27 characters, if the line ends with spaces, thereby making it impossible to split out correctly.
I've developed the following algorithm, which mostly works:
That is, if the remaining bit doesn't start with a space, steal the last character of the previous line.my @lines; while (length $_) { my $p=substr($=, 0, 1, ''); while ($p ne ' ') { print "Remaining text: $_\n"; print "p='$p'"; die "line to short, but no previous line?" unless length $lines[-1]; print "Previous line: $lines[-1]\n"; $_=substr($lines[-1], -1, 1, ''); $p=substr($_, 0, 1, ''); } }
Now, I'm sure there's a better way to implement this, proably with a regex that will just do it all for me, in a single line. I havn't been able to find it, however, which is no doubt because of my very poor regex-fu.
/ (.{0,27})/g, for some strange reason, seems to simply not match any chunks of less then 27 characters.
In reply to splitting text into lines -- code -> regex by theorbtwo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |