I have to modify some Perl scripts for a piped run and write a wrapper script to run them with a given set of input parameters. Before I can do that, I have to understand what is going in the first program. I need help deciphering this code:
# declare and initialise an empty hash
my %to_keep= ();
# an array
@line = ('some\one', 'two', 'three', 'four');
# trim the identifier
$line[0]=~s/\/[1]$//;
# store this into an array
@{$to_keep{$line[0]}{'1'}}=($line[1],$line[2]);
print @;
I’m familiar with the perl substitute function, s///. It goes:
s/text-regex_to_be_replaced/replacement/modifier.
However, I’m not too sure what the code above is doing. If I understand correctly, it replaces every occurrence of of ‘\’ with line[1], until the end of the string (indicated by the ‘$/’). Is this correct?
The other part I’m unsure about is the code below the ‘store’ comment. I think it’s storing a hash of array into an array. Can someone explain how the code works and what it prints out given the variables? Also, how can I retrieve the data I store in the array?
Bonus question: Can someone explain how modifying a perl script for a piped run works?
thanks
hmm, this is wired.
would will only match and remove /1 at the end of a string. So in your example it has no influence.
broken down, on the left side you got
$to_keep{'some\one'}{1}which is undefined in the example! But if we say it would give you the valuefoo, then you take this value and replaces in to@{foo}which basically means use the valuefooas the array name, hence@foo.on the right side you save the second and third element of
$lineas a list into this variable name,@foo.If we ignore the undefined and try to guess the intention, you got a script that defines its own variable names. Using the first element of list as the variable name, and setting it equal to the rest of the list