in reply to modify the contents of an array

I think this will help you

@arr = ('092205', '092305', '092605'); map {s/^(\d{2})(\d{2})(\d{2})/$1\/$2\/$3/} @arr; $" ="\n"; print "@arr";

Prasad

Replies are listed 'Best First'.
Re^2: modify the contents of an array
by blazar (Canon) on Sep 29, 2005 at 11:34 UTC
    No it won't. It will print the return values of those s///'s which happens to be 1.

    Also from perlstyle:

    Avoid using grep() (or map()) or `backticks` in a void context, that is, when you just throw away their return values. Those func- tions all have return values, so use them. Otherwise use a fore- ach() loop or the system() function instead.
      You would be right if prasadbabu had written:
      @arr = map {s/^(\d{2})(\d{2})(\d{2})/$1\/$2\/$3/} @arr;
      But he didn't. He rightly ignored the return values of s/// (if you write s/^(\d{2})(\d{2})(\d{2})/$1\/$2\/$3/ for @arr, you are ignoring the return value of s/// just as much as when using map in void context), and went of the side effects of s/// - the main feature of s///.
        You're perfectly right. I overlooked that. Hence here are my partial apologies to prasadbabu. Partial, because map still isn't the best tool for that task.

      blazar you are absolutely right. But the above coding works for me, i tested and then only posted.

      Hereafter i try to avoid these kind of mistakes.

      Prasad

Re^2: modify the contents of an array
by wolfger (Deacon) on Sep 30, 2005 at 12:58 UTC

    I'd have used foreach instead of map, but other than that... nice simple solution.