hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hello guys !
I have an array of numbers:
@numbers = (4, 5, 6); # not necessarly consecutive
and a string:
$str = 'test number X';
and I would like to get a new array:
@newArray = ('test number 4', 'test number 5', 'test number 6');
Is their a short way of doing that? I tried using map, but I guess I don't familiar with it enough.

Thanks.

Hotshot

Replies are listed 'Best First'.
Re: map question
by dragonchild (Archbishop) on Mar 19, 2002 at 16:32 UTC
    my @newArray = map { (my $newStr = $str) =~ s/X/$_/; $newStr } @number +s;
    Take some time to figure it out, then you'll understand. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: map question
by RMGir (Prior) on Mar 19, 2002 at 16:32 UTC
    I have this fear I'm answering a homework question, but I can't resist...
    @numbers = (4, 5, 6); # not necessarly consecutive $str = 'test number X'; my $fmt=$str; $fmt=~s/X/\%d/; my @newArray=map {sprintf $fmt,$_} @numbers; print join "\n",@newArray;
    By modifiying your format string to have "%d" it becomes something that sprintf can handle.

    Another alternative would be:

    @numbers = (4, 5, 6); # not necessarly consecutive $str = 'test number X'; my $fmt=$str; $fmt=~s/X//; my @newArray=map { "$fmt$_" } @numbers; print join "\n",@newArray;
    But that only works because your "X" is at the end of the string.
    --
    Mike
Re: map question (boo)
by boo_radley (Parson) on Mar 19, 2002 at 17:09 UTC
    like chihuahuas to a pork chop, we're drawn to this question...
    here's a novel way of doing it :
    @numbers = (6,210, 3); # not necessarly consecutive $templ="test X number "; print join "\n", map{($str = $templ)=~s/X/$_/?$str:undef}(@numbers);

    just remember that map is chief among the ob-fu master's tools. Use it wisely, and only after considering a for loop or foreach loop, or you may be writing some code that'll come back to haunt you.

Re: map question
by broquaint (Abbot) on Mar 19, 2002 at 16:34 UTC
    This saves having to use temporary variables within the loop, and does the same job.
    use strict; my $str = 'test number '; my @newArray = map { $str . $_ } 4 .. 6;

    HTH

    broquaint

      True, but it doesn't answer the exact question as stated. *grins* It's also the way I would've done it had I been coding that problem.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      That's only works if the digit I want to replace is the last character in the string, I guess my example wasn't good enough, I should have mentioned that

      Thanks.

      Hotshot
Re: map question
by tomhukins (Curate) on Mar 19, 2002 at 16:29 UTC

    You're on the right lines, map is the right way to go. Rather than directly giving you the answer, I'd like to ask you to show us the code you tried so we can gain more insight into what you find confusing about map's documentation.

    Update: Others have already answered your question, but it might be useful to see how and why you tried to use map to better understand how novices approach it. Also, it shows this isn't a "do my homework for me" question.

      I already started writing what I'v done and the answers stopped me in the middle, but here goes my old and wrong code:
      my $str = 'test number X'; my @numbers = (2, 3, 4); @newArray = map { ($_ = $str) =~ s/X/$_/ } @numbers;
      But I must add that I realized that wouldn't work even before I tried testing it, coz $_ in the regexp is no longer the one I expected.

      Thanks.

      Hotshot
Re: map question
by jmcnamara (Monsignor) on Mar 19, 2002 at 17:17 UTC

    Here is another method that may, or may not, be sufficient for your needs. As written, it also changes @numbers however:     my @newArray = map { s/(\d+)/test number $1/; $_ } @numbers;

    --
    John.