in reply to Re: Using map to Add a Line Breaks to a List
in thread Using map to Add a Line Breaks to a List

I like your recommended subtle fix. I like the elegant perlishness about it. I think I may edit my code to use it.

I don't understand how it works though. Can someone explain?
  • Comment on Re^2: Using map to Add a Line Breaks to a List

Replies are listed 'Best First'.
Re^3: Using map to Add a Line Breaks to a List
by Aristotle (Chancellor) on Mar 05, 2005 at 08:04 UTC

    As !1 explained: a successful pattern match in list context returns a list of captured matches, or an empty list if it didn't match. That's why your own map { /(.+)\./ } works — map provides list context to the block, so the pattern match returns the captured text. What I did is simply unconditionally add a "\n" to that list. So if the list was

    "foo.txt", "bar.bmp", "baz.pdf"

    then the result is

    "foo", "\n", "bar", "\n", "baz", "\n"

    which prompts print to duly produce the desired result.

    Makeshifts last the longest.

Re^3: Using map to Add a Line Breaks to a List
by !1 (Hermit) on Mar 05, 2005 at 06:17 UTC

    It's due to the capturing regex in list context. This returns a list of captured strings. Map then passes this list and the "\n" to print. Since $, = $\ = "", your output works the way it does. Of course, this will print blank lines if your filename doesn't contain a period.

      As will several other solutions. Good catch.

      Makeshifts last the longest.