CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Get csv data and format it to specific form dynamically.

 



ash1351
New User

Aug 24, 2015, 1:43 PM

Post #1 of 2 (1190 views)
Get csv data and format it to specific form dynamically. Can't Post

Hi, I have this front end CSS data stored in a web page. Here is the data in text file.


Code
secid,Initial_shares 
002826,3777
0028262,3777
0028262,3777
0028262,3777
0028262,3777
0028262,3777



I need to convert this text file into the below format. once, i convert it to below format, i will be able to display it on the front end. this format below is used to display data in jqgrid.




Code
var secid = 
[
"002826", "0028262", "0028262", "0028262", "0028262", "0028262"];
var Initial_shares =
[
"3777", "3777", "3777", "3777", "3777", "3777"1
];




In order to convert the text to above format, i have used the below perl code. Please note. This conversion is static. i.e. it knows how many columns are there. In this case, there will be 2 columns. secid and Initial_shares. so here is the static code



Code
my @a=(); 
my @b=();

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

my @perl_array=();
my @filena = split(/\|/, $filename);



open (TXT, "<$filename") || die "Can't open $filename: $!\n";
while (my $line=<TXT>) {
chomp($line);
($a2, $b2) = split /,/, $line;

push @a,('"',"$a2",'"',',');
push @b,('"',"$b2",'"',',');
}



splice @a,0,4; #this splice is used to remove the header name. i.e. first row data- secid, Initial_shares
splice @b,0,4;


push @a1,"var secid=[@a]";
push @b1,"var Initial_shares=[@b]";

push @perl_array, "@a1; @b1";
close TXT;



The @perl_array will be then exactly similar to the kind of data we were expecting at the start. i wil transfer this perl variable to front end for displaying then.

I need help in the following case. What if instead of 2 columns, there are 5 columns. How can we convert the same csv file to the format mentioned earlier. it should be all dynamic. Can someone shed some light please.


Zhris
Enthusiast

Aug 24, 2015, 9:28 PM

Post #2 of 2 (1167 views)
Re: [ash1351] Get csv data and format it to specific form dynamically. [In reply to] Can't Post

Hi,

Its usually advisable to parse CSV data using a relevant module such as Text::CSV. You'll find methods available that deal with certain complexities you are facing. I'll leave this to you to explore.

Array and hash data structures can be multi-dimensional, in that their elements can be references to other data structures, What you probably need is a hash of arrays, where the keys are headings and the values are array references to each columns values. You would first parse the headings row into a separate array, then while reading the body of the CSV, iterate over these headings, pushing each corresponding value into the hash of arrays ( push @{ hash->{ heading } }, value ). Its then a case of converting the hash of arrays into a string of javascript arrays.


Code
use strict; 
use warnings;
use Data::Dumper;

#open my $handle, '<', $path or die "cannot open '$path': $!"
my $handle = \*DATA;

my $csv = parse_csv ( $handle );
my $jqgrid = build_jqgrid( $csv );

print Dumper $csv;
print "\n", $jqgrid;

sub parse_csv
{
my $handle = shift;

my $csv = { };

my $headings = parse_row( $handle );

while ( my $values = parse_row( $handle ) )
{
push @{$csv->{$headings->[$_]}}, $values->[$_] for ( 0 .. $#$headings );
}

return $csv;
}

sub parse_row
{
my $handle = shift;

my $row = <$handle>;

if ( defined $row )
{
#chomp $row;
$row =~ s/\s*$//;

my @row = split /,/, $row;

return \@row;
}
}

sub build_jqgrid
{
my $csv = shift;

my $jqgrid;

while ( my ( $heading, $values ) = each %$csv )
{
$jqgrid .= "var $heading =\n[\n\t" . ( join ', ', map { qq/"$_"/ } @$values ) . "\n];\n";
}

return $jqgrid;
}

__DATA__
secid,Initial_shares
002826,3777
0028262,3777
0028262,3777
0028262,3777
0028262,3777
0028262,3777


Output:

Code
$VAR1 = { 
'Initial_shares' => [
'3777',
'3777',
'3777',
'3777',
'3777',
'3777'
],
'secid' => [
'002826',
'0028262',
'0028262',
'0028262',
'0028262',
'0028262'
]
};

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


Chris


(This post was edited by Zhris on Aug 24, 2015, 9:28 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives