in reply to Dividing a string into multiple substrings of different length
The unpack approach used by BrowserUk above is IMHO quite elegant and attractive, not to mention efficient, but a regex-extraction approach offers one further possible advantage: data validation.
Here, the validation/extraction regex is defined separately and passed to the function. Rather than the dummy used in the example code, it can be highly specific to the data. One can imagine a further elaboration in which the passed regex is optional and a default regex is used in the subroutine if no regex is passed:c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = 'abcdefghijklmno'; ;; my $validate = qr{ \A (....) (..) (..) (.....) (..) \z }xms; ;; my @ra = Suc($s, $validate); dd \@ra; ;; sub Suc { my ($DC, $rx_valid) = @_; ;; my @ra = $DC =~ $rx_valid; @ra or die qq{bad format: '$DC'}; ;; return map uc($_), @ra; } " ["ABCD", "EF", "GH", "IJKLM", "NO"]
Give a man a fish: <%-(-(-(-<
|
|---|