1. Create variablenames dynamically
An example of dynamically created variable names can be found in e.g. Javascript programming:
In Perl you should not do this, then Perl has an excellent answer: a hash!let k = 'value'; let i = 0; for (i = 0; i < 5; i++) { eval('var ' + k + i + ' = ' + i + ';'); #result e.g. var value0 = 0; } console.log("value0 = " + value0); console.log("value1 = " + value1); console.log("value2 = " + value2); console.log("value3 = " + value3); console.log("value4 = " + value4);
%value=(); for ($i = 0; $i < 5; $i++) { $value{$i} = $i; } for ($i = 0; $i < 5; $i++) { print("value$i = $value{$i}\n"); }
Notice that the curly braces in$str = "A B C"; $str =~ s/([A-Z]+)/${1}0/g; print("Result: $str\n"); A0 B0 C0