If you use Text::CSV_XS' csv function with the filter attribute, you don't also keep the CSV data and stream the required content right into the arrays:

use 5.14.2; use warnings; use Text::CSV_XS qw(csv); my (@a, @b); my $aoa = csv ( in => *DATA, headers => "skip", filter => { 1 => sub { push @a, $_; 1 }, 2 => sub { push @b, $_; 0 }, }); { local $" = ", "; say "var secid = [@a]"; say "var Initial_shares = [@b]"; } __END__ secid,Initial_shares 002826,3777 0028262,3777 0028262,3777 0028262,3777 0028262,3777 0028262,3777

var secid = [002826 0028262 0028262 0028262 0028262 0028262] var Initial_shares = [3777 3777 3777 3777 3777 3777]

The entries in the filter will be used in ascending order. All but the last should return a true value. The last should return a false value to reject the record: You already stored all required data in the two arrays.

update: Alternatively, you can put everything in a hash:

my %h; my $aoa = csv ( in => *DATA, headers => "auto", filter => { 1 => sub { for my $f (keys %_) { push @{$h{$f}}, $_{$ +f} }; 0 }}, );

{ Initial_shares => [ 3777, 3777, 3777, 3777, 3777, 3777 ], secid => [ '002826', '0028262', '0028262', '0028262', '0028262', '0028262' ] }

Enjoy, Have FUN! H.Merijn

In reply to Re^2: Get CSV data and convert it to other format data dynamically. by Tux
in thread Get CSV data and convert it to other format data dynamically. by ash1351

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.