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

my @a=(); my @b=(); my @a1=(); my @b1=();

Whenever you feel the need to declare variables like @a,@b,@c,... or @a1,@a2,... it is almost always better to use a container, specially if you want to extend those items without having to declare @d,@e or @a12,@a13 later on.

This also greatly simplifies your code. You want an array of columns, without having to know how many columns there will be? Use a two-dimensional array, i.e. an array of arrays. This is done with references in perl - see perlref.

open (TXT, "<$filename") || die "Can't open $filename: $!\n"; my @array; while (my $line=<TXT>) { chomp($line); my @list = split /,/, $line; # now distribute the elements into each column's array for ( 0 .. $#list ) { push @{$array[$_]}, $list[$_]; } }

Your @array now looks like this:

@array = ( [ 'secid', '002826', '0028262', '0028262', '0028262', '0028262', '0028262' ], [ 'Initial_shares', '3777', '3777', '3777', '3777', '3777', '3777' ] );

Now populate @perl_array with the stringified elements of @array:

my @perl_array; foreach $array_ref ( @array ) { my $colname = shift @$array_ref; my $items = join ",", @$array_ref; push @perl_array, "var $colname=[$items]"; } for ( @perl_array ) { print $_,"\n"; }

Output:

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

Adding more columns to your data results in more elements in @perl_array. Using as input

secid,Initial_shares,Char 002826,3777,a 0028262,3777,b 0028262,3777,c 0028262,3777,d 0028262,3777,e 0028262,3777,f

results in

var secid=[002826,0028262,0028262,0028262,0028262,0028262] var Initial_shares=[3777,3777,3777,3777,3777,3777] var Char=[a,b,c,d,e,f]

And yes, you should use Text::CSV or such as mentioned elsewhere.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'