title: Split string into variables and use output as element
body: <p>Well.. I'm stuck again. I've read up quite a few topic with similar problems but not finding a solution for mine. I have a ; delimited csv file and the strings at the 8th column ($elements[7]) is as following: "aaaa;bb;cccc;ddddd;eeee;fffff;gg;". What i'm trying is to split the string based on ; and capture the outputs to variables. Then use those variables in the main csv file in their own column.</p>
  
  <p>So now the file is like:</p>
  
  <p>3d;2f;7j;8k;4s;2b;5g;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";4g;1a;5g;2g;7h;3d;2f;7j
  3c;9k;5l;4g;1a;5g;3d;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";4g;1a;5g;2g;7h;3d;2f;7j
  4g;1a;5g;2g;7h;3d;8k;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";3d;2f;7j;8k;4s;2b;4g;1a</p>
  
  <p>And i want it like:</p>
  
  <p>3d;2f;7j;8k;4s;2b;5g;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg
  3c;9k;5l;4g;1a;5g;3d;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg;
  4g;1a;5g;2g;7h;3d;8k;3d;2f;7j;8k;4s;2b;4g;1a;aaaa;bb;cccc;ddddd;eeee;fffff;gg;</p>
  
  <p>This is my code i've been trying it with. I know.. it's terrible! But i'm hoping someone can help me?</p>
  
  <pre><code>use strict;
  use warnings;
  
  my $inputfile  = shift || die "Give files\n";
  my $outputfile = shift || die "Give output\n";
  
  open my $INFILE,  '&lt;', $inputfile   or die "In use / Not found :$!\n";
  open my $OUTFILE, '&gt;', $outputfile  or die "In use :$!\n";
  
  while (&lt;$INFILE&gt;) {
    s/"//g;
    my @elements = split /;/, $_;
  
      my ($varA, $varB, $varC, $varD, $varE, $varF, $varG, $varH) split (';', $elements[10]);
          $elements[16] = $varA;
          $elements[17] = $varB;
          $elements[18] = $varC;
          $elements[19] = $varD; 
          $elements[20] = $varE;
          $elements[21] = $varF;
          $elements[22] = $varG;
          $elements[23] = $varH;
  
  my $output_line = join(";", @elements);
  print $OUTFILE $output_line;
  }
  
  close $INFILE;
  close $OUTFILE;
  
  exit 0;
  </code></pre>
  
  <p>I'm confused about the my statement as well, it shouldn't be possible right? I mean the $vars are in a closed part so it shouldn't be possible to write them to $elements? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is how i adjusted the code with TLP's suggestions:</p>
  
  <pre><code>use strict;
  use warnings;
  use Text::CSV;
  
  my $inputfile  = shift || die "Give files\n";
  my $outputfile = shift || die "Give output\n";
  
  open my $INFILE,  '&lt;', $inputfile   or die "In use / Not found :$!\n";
  open my $OUTFILE, '&gt;', $outputfile  or die "In use :$!\n";
  
  my $csv = Text::CSV-&gt;new({  # create a csv object
      sep_char =&gt; ";",    # delimiter
      eol =&gt; "\n",        # adds newline to print
  });
  
  while (my $row = $csv-&gt;getline($INFILE)) {      # $row is an array ref
  my $line = splice(@$row, 10, 1);            # remove 8th line
  $csv-&gt;parse($line);                         # parse the line
  push @$row, $csv-&gt;fields();                 # push newly parsed fields onto       main array
  $csv-&gt;print($OUTFILE, $row);
  }
  
  close $INFILE;
  close $OUTFILE;
  
  exit 0;
  </code></pre>