Michael P. Broida (michael.p.broida@boeing_oops.com) wrote on
MMMDCLXXXVIII September MCMXCIII in <URL:'' Abigail wrote:
'' >
'' > Michael P. Broida (michael.p.broida@boeing_oops.com) wrote on
'' > MMMDCLXXXIII September MCMXCIII in <URL:'' > ,, Abigail wrote:
'' > ,, >
'' > ,, > Matija Papec (
[email protected]) wrote on MMMDCLXXXIII September MCMXCIII
'' > ,, > in <URL:'' > ,, > --
'' > ,, > -- I went through perldoc but didn't found similar regex,
'' > ,, > -- print join ',', 'a bb ccc dddd' =~ /(\w)+/g;
'' > ,, > --
'' > ,, > -- the question is, what it exactly matches and why?
'' > ,, >
'' > ,, > /(\w)+/ matches a set of consecutive word characters, capturing
'' > ,, > the *last* one. //g in list context means, do this as often as
'' > ,, > possible (without overlap), returning a list of each of the submatches.
'' > ,, >
'' > ,, > So, 'a bb ccc dddd' =~ /(\w)+/g; returns for each substring of
'' > ,, > consecutive word characters the last one, resulting in 'a', 'b', 'c' and 'd'.
'' > ,,
'' > ,, That tests out as you said, so it's MY thinking that's off.
'' > ,, Hopefully, you can clue me in.
'' > ,,
'' > ,, I expected it to result in "a,bb,ccc,dddd". Now I realize that
'' > ,, it's the positioning of the + that causes it to get a single
'' > ,, character from each group. If the + is inside the (), it
'' > ,, prints what I expected.
'' > ,,
'' > ,, But... What is causing the original /(\w)+/ to get the LAST
'' > ,, character from each group instead of the FIRST character from
'' > ,, each group?
'' >
'' > Would you expect:
'' >
'' > $x = $_ for qw /a b c d/
'' > print $x;
'' >
'' > to print 'a' as well?
''
'' It doesn't print anything without a semi-colon on the first line.
'' <grin>
''
'' At first glance, I thought it would print each letter. Then I
'' looked deeper and realized it's basically assigning and re-assigning
'' $x (via $_) during the "for" loop, but only printing it when it's all
'' done. Thus it only prints "d".
''
'' But the prior discussion was about a regex, not a "for" loop.
'' If your point is that the regex processing works similarly to
'' the "for" loop in your example, then I see what you mean.
''
'' If that's NOT what your point was, then you've lost me. <grin>
My point is, if you repeatedly assign something to a variable, do you
expect the variable to retain the first value it was set to, or the
last value? Because that's happening in both the match, and the for loop.