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: [gopsi1234] Generate a C file from CSV file using perl: Edit Log



Zhris
Enthusiast

Jun 21, 2014, 2:46 AM


Views: 60623
Re: [gopsi1234] Generate a C file from CSV file using perl

Hi,


Quote
what modification has to be done inside the while loop so that the cmd column is not printed and the rest of the information is printed in the output file as below


Just remove cmd's index from the slice, and also a %s from the format pattern.

Code
printf "{%s, FLASH_CONFIGURATION_DEVICE(%s, %s, %s, %s, %s, %s, %s, %s)}\n",    
(split /, /)[1..3,5..9,0];



Quote
Actually the END statement was used for easy parsing, but it seems it is not required in the csv file.


A possibility could be to simply re-adjust the condition to match the appropriate rows. In the example below, I have made the assumption that rows we need begin with "0x".

Code
#!/usr/bin/perl   
use strict;
use warnings;

while ( <DATA> )
{
if ( /^0x/ )
{
s/\s+$//;

printf "{%s, FLASH_CONFIGURATION_DEVICE(%s, %s, %s, %s, %s, %s, %s, %s)}\n",
(split /, /)[1..3,5..9,0];
}
}

__DATA__
vendorid, memid, size, devid, cmd, addr, mode, dummy, conf_wid, rate

CHIP XXX
0xBF, 0x2501, 256, yyy, 1, 3, 0, 0, 0, 20

CHIP YYY
0xBF, 0x2501, 512, yyy, 1, 3, 0, 0, 0, 20



CHIP WWW

0xBF, 0x2501, 1024, yyy, 1, 3, 0, 0, 0, 20


If the input format is guaranteed regular then an alternative might be to set the input record separator to double newline and process each chunk:

Code
#!/usr/bin/perl   
use strict;
use warnings;

$/ = "\n\n";

while ( <DATA> )
{
next if $. == 1; # strip off head.

s/\s+$//;

printf "{%s, FLASH_CONFIGURATION_DEVICE(%s, %s, %s, %s, %s, %s, %s, %s)}\n",
(split /\n|, /)[2..4,6..10,1];
}

__DATA__
vendorid, memid, size, devid, cmd, addr, mode, dummy, conf_wid, rate

CHIP XXX
0xBF, 0x2501, 256, yyy, 1, 3, 0, 0, 0, 20

CHIP YYY
0xBF, 0x2501, 512, yyy, 1, 3, 0, 0, 0, 20

CHIP WWW
0xBF, 0x2501, 1024, yyy, 1, 3, 0, 0, 0, 20


Or if there are no double newlines, read two rows at a time:


Code
#!/usr/bin/perl   
use strict;
use warnings;

while ( <DATA> )
{
next if $. == 1; # strip off head.

my $next_row = <DATA>;
$next_row =~ s/\s+$//;

printf "{%s, FLASH_CONFIGURATION_DEVICE(%s, %s, %s, %s, %s, %s, %s, %s)}\n",
(split /, /, $next_row)[1..3,5..9,0];
}

__DATA__
vendorid, memid, size, devid, cmd, addr, mode, dummy, conf_wid, rate
CHIP XXX
0xBF, 0x2501, 256, yyy, 1, 3, 0, 0, 0, 20
CHIP YYY
0xBF, 0x2501, 512, yyy, 1, 3, 0, 0, 0, 20
CHIP WWW
0xBF, 0x2501, 1024, yyy, 1, 3, 0, 0, 0, 20


Or even:


Code
#!/usr/bin/perl    
use strict;
use warnings;

while ( <DATA> )
{
next if $. == 1 or not $. % 2;

s/\s+$//;

printf "{%s, FLASH_CONFIGURATION_DEVICE(%s, %s, %s, %s, %s, %s, %s, %s)}\n",
(split /, /)[1..3,5..9,0];
}

__DATA__
vendorid, memid, size, devid, cmd, addr, mode, dummy, conf_wid, rate
CHIP XXX
0xBF, 0x2501, 256, yyy, 1, 3, 0, 0, 0, 20
CHIP YYY
0xBF, 0x2501, 512, yyy, 1, 3, 0, 0, 0, 20
CHIP WWW
0xBF, 0x2501, 1024, yyy, 1, 3, 0, 0, 0, 20


etc etc etc...

Since you don't use the CHIP *** rows, perhaps they could also be removed, producing a much more regular CSV file to have to deal with:


Code
__DATA__   
vendorid, memid, size, devid, cmd, addr, mode, dummy, conf_wid, rate
0xBF, 0x2501, 256, yyy, 1, 3, 0, 0, 0, 20
0xBF, 0x2501, 512, yyy, 1, 3, 0, 0, 0, 20
0xBF, 0x2501, 1024, yyy, 1, 3, 0, 0, 0, 20


Chris


(This post was edited by Zhris on Jun 21, 2014, 4:08 AM)


Edit Log:
Post edited by Zhris (Enthusiast) on Jun 21, 2014, 2:47 AM
Post edited by Zhris (Enthusiast) on Jun 21, 2014, 2:48 AM
Post edited by Zhris (Enthusiast) on Jun 21, 2014, 2:49 AM
Post edited by Zhris (Enthusiast) on Jun 21, 2014, 3:00 AM
Post edited by Zhris (Enthusiast) on Jun 21, 2014, 4:08 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