abhishes has asked for the wisdom of the Perl Monks concerning the following question:
In my program I have two functions. Both are almost the same except that one extracts the value of $1 from the regex m/\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*/ and the other extracts the value of $2.
So I thought why not merge these into one function. and pass the column number which I want to extract as a parameter?
So I wrote code like
but I don't like this ifelse approach. Can't I do it shorter by avoiding this if else?if ($columnNumber == 2) { push @sps, $2 if ($2); } elsif ($columnNumber == 3) { push @sps, $3 if ($3); }
So that the columnNumber automatically substitues itself inside the $1/$2/$3 expression.push @sps, $($columnNumber) if ($($columnNumber));
|
|---|