-4

I want to replace {x} where x is a number from 1-10 with a string from an array. The array is populated by splitting a string with whitespace.

I have put together some code but the regex is probably wrong.

my @params = split(' ', "Paramtest: {0} {1} {2}"); 
my $count = @params;
for (my $i = 0; $i <= $count; $i++) {
    my $param = @params->[$i];
    $cmd_data =~ s/{"$i"}/"$param"/;
    if(!$cmd_data) {
        $server->command(sprintf("msg $target %s incorrect syntax for %s.", $nick, "!params p1 p2 p3"));
        return;
    }
}
$server->command(sprintf("msg $target %s.", $cmd_data));

Update

I've tried using the below code as a modified version of Miller's (the first answer)

my @params = split(' ', "!fruit oranges apples"); 
my $cmd_data = "Fruits: {0} {1}";
$cmd_data =~ s{\{(\d+)\}}{
    $params[$1] // die "Not found $1" #line 160
}eg;

$server->command(sprintf("msg $target %s.", $cmd_data));

Output

Not found 1 at myscript.pl line 160.
Miller
  • 34,962
  • 4
  • 39
  • 60
NullBy7e
  • 536
  • 9
  • 23
  • "but the regex is probably wrong." Did you try it? What happens? – aliteralmind Jul 09 '14 at 23:05
  • @aliteralmind nothing happens, the command_data gets printed without anything replaced and no syntax error. – NullBy7e Jul 09 '14 at 23:12
  • `use strict; use warnings;` is your friend. – TLP Jul 09 '14 at 23:12
  • @tlp those two are both enabled, the above code is just a part of the entire script. – NullBy7e Jul 09 '14 at 23:13
  • @NullBy7e Yes, I noticed that `@params->[$i]` is not a strict violation, it does give a "deprecated" warning, however. The correct syntax is `$params[$i]`. – TLP Jul 09 '14 at 23:19
  • @TLP indeed, I have fixed that now, however the the problem remains. I did a dump of @ params and this was the output: $VAR1 = 'params'; – NullBy7e Jul 09 '14 at 23:23
  • I don't quite understand what you are doing in your code. You take a string which contains the `{0} {1}` numbers and split into `@params` -- presumably this is what you want to put into a string. Then you loop over that array, and edit some other variable, searching for the strings that are already in `@params` and replacing them with themselves (although you will get `{0}` replaced with `Paramtest:` and `{1}` with `{0}`). Assuming you had not bungled the split, how would you know that your substitution worked? – TLP Jul 09 '14 at 23:24
  • @NullBy7e Nope, with this code it would be impossible for `@params` to contain the string `'params'` – TLP Jul 09 '14 at 23:27
  • 2
    @NullBy7e It is considered rather impolite to change your question completely after getting feedback. You've even incorporated Miller's code, so it is hard to tell that *he* is the author of the code and not you. – TLP Jul 09 '14 at 23:40
  • @TLP true but then again if i make another question I just get more downvotes. – NullBy7e Jul 09 '14 at 23:43
  • That code does not cause that error message. – TLP Jul 09 '14 at 23:44
  • @TLP totally my fault, I was using the cmd name rather than message data. I really have to get better with debugging things myself x) – NullBy7e Jul 09 '14 at 23:46
  • 2
    @NullBy7e The mistake you are making and getting downvoted for is that you are asking a question about the problem you are trying to solve using Perl, when you should be asking a question about Perl. People downvote questions like this because you are asking for programmers to work for free. It can be a difficult distinction to make when you are new, but when you can replace all the code you previously used it is no longer about the code, it is about the problem. A nice rule of thumb is to create an [sscce](http://sscce.org/). – TLP Jul 10 '14 at 00:01

1 Answers1

3

Perhaps a more generalized search and replace will serve you better:

use strict;
use warnings;

my @params = qw(zero one two three four five six seven eight);

my $string = 'My String: {0} {1} {2}';

$string =~ s{\{(\d+)\}}{
    $params[$1] // die "Not found $1"
}eg;

print $string;

Outputs:

My String: zero one two
Miller
  • 34,962
  • 4
  • 39
  • 60
  • 1
    @NullBy7e: If you are getting the message `Not found 1` then you are not running the code that you show. It means that `@params` has less than two elements, which it cannot have since it is set up with three elements on the first line – Borodin Jul 09 '14 at 23:44