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: Need a Custom or Prewritten Perl Program?: I need a program that...: Re: [Zhris] Generate a C file from CSV file using perl: Edit Log



gopsi1234
Novice

Jul 1, 2014, 7:43 AM


Views: 50687
Re: [Zhris] Generate a C file from CSV file using perl

Hi Zhris here is my perl code

Code
open (DATA, "<input.csv") or die "Can't open  $!"; 
my %data;
my @names;
while(<DATA>){
my @list=split(/,/);
for(my $i=0; $i<=$#list;$i++){
if($.==1){
$name[$i]=$list[$i];
}

else{
push @{$data{$name[$i]}},$list[$i];

}
}
}
foreach(keys %data){
printf "{%s, %s\n}", @{$data{vendorid}}, @{$data{memid}};
}


Code
And i am facing trouble printing two values currently from the csv files. The output is printing as 
{0xBF, 0xBF
}{0xBF, 0xBF
}{0xBF, 0xBF
}{0xBF, 0xBF
}{0xBF, 0xBF
}{0xBF, 0xBF
}{0xBF, 0xBF
}{0xBF, 0xBF
}{0xBF, 0xBF
}{0xBF, 0xBF
}{0xBF, 0xBF


where as it should be

Code
0xBF,0x5501 
0xBF,0x5502

Moreover

1. I could do it with Text::CSV_XV but I cannot use Text::CSV_XV as because the module is not installed in our system and i cannot change the perl version installed.
2. Now the input file is as below

Code
vendorid,memid,size,devid,cmd,addr,conf_width,dummy,mode,rate,devicename 
0xBF,0x5501,32,est,1,3,0,0,0,20,xxx
0xBF,0x5502,64,est,1,3,0,0,0,20,yyy
0xBF,0x5503,128,est,1,3,0,0,0,20,zzz

As you can see there is no gaps/space in the input file between each column, as because whenever i was putting space, the parsing is not happening and an error is throwing. I am unable to tackle this issue. I want flexibility in terms of spaces i give between column, but not finding the right perl code to tackle that.

So, these are issues, can u please go through the code once, and tell me the changes i need to do to get the below output

Code
{0x5501, FLASH_CONFIGURATION_DEVICE(32, est, 3, 0, 0, 0, 20, 0xBF)} 
{0x5502, FLASH_CONFIGURATION_DEVICE(64, est, 3, 0, 0, 0, 20, 0xBF)}



(This post was edited by gopsi1234 on Jul 1, 2014, 7:47 AM)


Edit Log:
Post edited by gopsi1234 (Novice) on Jul 1, 2014, 7:45 AM
Post edited by gopsi1234 (Novice) on Jul 1, 2014, 7:47 AM


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

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