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'

In reply to Re: Get CSV data and convert it to other format data dynamically. by shmem
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.