I would like to optimise this Perl sub:
push_csv($string,$addthis,$position);
for placing strings inside a CSV string.
e.g. if $string="one,two,,four"; $addthis="three"; $position=2;
then push_csv($string,$addthis,$position)
will change the value of $string = "one,two,three,four";
sub push_csv {
my @fields = split /,/, $_[0]; # split original string by commas;
$_[1] =~ s/,//g; # remove commas in $addthis
$fields[$_[2]] = $_[1]; # put the $addthis string into
# the array position $position.
$_[0] = join ",", @fields; # join the array with commas back
# into the string.
}
This is a bottleneck in my code, as it needs to be called a few million times.
If you are proficient in Perl, could you take a look at it, and suggest optimisation/alternatives? Thanks in advance! :)
EDIT: Converting to @fields and back to string is taking time, I just thought of a way to speed it up where I have more than one sub call in a row. Split once, then push more than one thing into the array, then join once at the end.