in reply to using map

One could use map as in
    my @digits = map { $_ =~ m{ \d }xmsg } $list;
but it would be, IMHO, simpler, clearer and more idiomatic as

>perl -wMstrict -le "my $list = '1, 23-456 7890'; my @digits = $list =~ m{ \d }xmsg; printf qq{'$_' } for @digits; " '1' '2' '3' '4' '5' '6' '7' '8' '9' '0'

(Although, to echo JavaFan, it's not really clear just what you want.)

Replies are listed 'Best First'.
Re^2: using map
by jwkrahn (Abbot) on May 06, 2012 at 03:50 UTC
    printf qq{'$_' } for @digits;

    The correct format for printf is printf FORMAT_STRING, LIST so that should be:

    printf q{'%s' }, $_ for @digits;

    But you don't really need the extra overhead of using printf:

    print qq{'$_' } for @digits;

    But that wouldn't work properly because of your use of the -l switch which will add a newline after every print.    Of course you could do something like this:

    print join ' ', map "'$_'", @digits;

    Which also avoids the extra space character at the end of the line.

      The correct format for printf is printf FORMAT_STRING, LIST so that should be:

      Any ordinary (non-specifier) characters in the format string are output verbatim.

      If the format contains no %specifiers, then just what is in the template gets output.

      As -l doesn't affect printf, it makes it a convenient way of printing output that does not get an automatic \n appended for those rare occasions when that is required.

      It is convenient, safe and a lot easier than having to add \n to every darn print statement.

      Of course, say ought to be a simple alternative to -l, but a) they made it so darn inconvenient to use; b) its a pain in the neck having to switch back to print any time you need to test something under a pre-5.10 version.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        If the format contains no %specifiers, then just what is in the template gets output.

        And so printf $_ acts exactly like print $_.

        Very clever... until $_ happens to contain something that looks like a printf specifier and it comes back to bite you.

        Of course, say ought to be a simple alternative to -l, but a) they made it so darn inconvenient to use

        For one-liners, say is actually very convenient to use. Just replace "-e" with "-E".

        perl -E"say q(Hello world)"
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        You're correct in this case (with the warnings you added afterwards), yes, but leaving out the "%s" was the very reason of thousands of format string vulnerabilities everywhere. And even though that class of vulnerabilities don't affect Perl, it would be correct and a good practice to include it anyway, which is what jwkrahn was trying to point out.

        I do not think that you pointing out that the extra care is unnecessary in this very case was very beneficial to the whole discussion.

      The correct format for printf ...
      ... wouldn't work properly because of your use of the -l switch ...

      The -l switch is hard-wired into the little command-line code formatter I use because I never got around to making the various switches, module inclusions, etc., optional. It's on my to-do list. As a consequence, as you point out, the use of  print() would have given me a 'vertical' listing, which I did not want. Instead of tediously editing out the -l every time I tested an example, I just used  printf() even though it's just a tad inappropriate.

      Which also avoids the extra space character at the end of the line.

      I was feeling wildly extravagant when I wrote the example code.