in reply to Re: Get CSV data and convert it to other format data dynamically.
in thread Get CSV data and convert it to other format data dynamically.

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