in reply to Re^3: Combinations of lists, etc
in thread Combinations of lists to a hash
And how is '' => '}' replacing the line ending? I know \z matches the end, but those empty 'quotes' puzzle me.
That's easy enough to explain. In essence, it's a bit of regex affectation. The idea was to stick a }-curly on the end of a string. That can be done well enough with a
$string .= '}';
statement, but I thought it would be neat to it with the same global substitution that was handling all the other transformations. The empty pattern matches the empty string, and the empty string is present n+1 times in a string of n length.
And there's always an empty string at the \z (absolute end) of any string, even the empty string! (And the empty string is a perfectly good hash key.)c:\@Work\Perl\monks\tel2>perl -wMstrict -le "my $s = '123'; $s =~ s{ }{X}xmsg; print qq{A: '$s'}; ;; $s = ''; $s =~ s{ }{X}xmsg; print qq{B: '$s'}; ;; $s = '123'; $s =~ s{ \z }{X}xmsg; print qq{C: '$s'}; " A: 'X1X2X3X' B: 'X' C: '123X'
As you have seen, this approach breaks down when you try to replace an empty string at different locations with different replacement strings: there can only be one ''-keyed value in a hash. To do what you want with a regex, you'd have to resort to some screaming hack like (untested)
(in the unlikely case your input string always starts with a fixed sequence like 'P'), but if you just want to add different stuff at the start and end of a string, it would be better IMHO to just do something likemy %globize = ('P' => '{P', ':' => '}:{', '' => '}'); ... $globule =~ s{ (\A P | : | \z) }{$globize{$1}}xmsg;
Give a man a fish: <%-{-{-{-<
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: Combinations of lists, etc
by tel2 (Pilgrim) on Oct 07, 2019 at 02:13 UTC |