in reply to Replace arrays in external file

And you probably meant eval($a[1] . $a[0] . $a[2]); rather than eval(@a[1] . @a[0] . @a[2]);

Or use eval("$a[1]$a[0]$a[2]"); It will save you a few keystrokes.

Your program then can become:

use Modern::Perl; while (<DATA>) { do {print; next} unless /^eval/; s/(?<=\[)\d+(?=])/int(rand(99999))/ge; print; } __DATA__ @a = ("'hello world'",'print ', ';'); eval("$a[1]$a[0]$a[2]");

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Replace arrays in external file
by astroid96 (Initiate) on Apr 10, 2012 at 16:15 UTC
    i may have forgotten to mention that the array indexes must match, lets say @a1 was changed to "@a3455" all @a1's should be changed to match that one. my original code doesnt like those dots in the eval statement.
      I showed you an alternative way of writing your eval which does not need any "dots".

      To make the "random" values stick to the elements of the @a array, try this:

      use Modern::Perl; my @replace = map int(rand(99999)), (0 .. 10); while (<DATA>) { do {print; next} unless /^eval/; s/(?<=\[)(\d+)(?=])/$replace[$1]/ge; print; } __DATA__ @a = ("'hello world'",'print ', ';'); eval("$a[1]$a[0]$a[2]");

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics