http://www.perlmonks.org?node_id=629294


in reply to Suggestions for simplifying script to parse csv data

It looks like you might have some mistakes in your explanation (e.g. your first "parsing rule" refers to "FWIA770", but your sample output data contains "FWA1770"), and there are some unexplained details in the data. (E.g. where do output strings like "FTSE" and "AWBRIC" come from? I didn't see these in the input or code as originally posted.)

And you don't mention whether the ordering of array elements in the output AoH is important -- if the results need to be in a particular order, this affects how complicated or simple the script can get.

I gather that parsing the CSV input isn't the main thing you're asking about. You want to know how to simplify and shorten the code you posted. The basic rule to apply there is: figure out how to eliminate repeated lines of code. As originally posted, your code contained the same set of assignment statements repeated in two or three different places, and this is most likely unnecessary. The code would be simpler, clearer, and easier to maintain/update if you reorganize or refactor it so that the repetitions aren't needed there are no repetitions.

And apart from that, don't use a reference to a hash when just a simple hash will do (i.e. replace  $row = {}; with  %row = (); and then when pushing row hashes onto your output array, do it like this:  push @IndxData, { %row };

Finally, you might decide that some of your temporary-storage variables aren't needed at all (e.g. do  for ( split /\|/, $Currency ) instead of assigning the split to an array just to loop over the array).