I have a 17 MB CSV file that needs to be parsed. So I parsed the CSV file and saved it as multidimensional array
while (my $line = < $in_ph > ) { chomp $line; my@ fields = split(/,/, $line);@ key = split(/_/, $fields[0]); $data[$count][0] = $fields[1]; foreach(@key) { $data[$count][$k + 1] = $key[$k]; $k++; } $k = 0; $count++; }
I know need to access only certain elements of each row and column. I am running out of memory during this action. Please help me out with this
my $n = 2033; my $color = $data[$n][0]; my $order = $data[$n + 1][0]; my $shape = $data[$n + 3][0]; my $name = $data[$n + 4][0]; foreach my $aref(@data[2033..$#data]) { if (($aref - $n) % 6267 == 0) { $color = $data[$aref][0]; $order = $data[$aref + 1][0]; $shape = $data[$aref + 3][0]; $name = $data[$aref + 4][0]; } print $out_ph1 $predata, ",", $color, ",", $order, ",", $shape, ", +", $name, ","; foreach(@{ $aref }[4..$#$aref]) { print $out_ph1 $_, "\_"; } print $out_ph1 "\n"; }
I need parse data from row 2033 onwards. I have initialised my color, order, shape and name to certain values from my @data. I know have a for loop that traverses from row 3022 to the end of @data. After every 6267 rows traversed, I want to change the value of color, order, shape and name. I am using the if loop for the same. At the same time, I want color, order, shape and name joined with data from column 4 onwards for each row. I keep getting the warning use of uninitialized value in join for the below line
print $out_ph1 $predata, ",", $color, ",", $order, ",", $shape, ",", $ +name, ",";
I am also running out of memory before the loop completes. Please help me! I am not sure if the way I have parsed the CSV file is causing this or if the way I am displaying this is.

In reply to Running out of memory while running this for loop by Ppeoc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.