
Zhris
Enthusiast
Jun 21, 2014, 2:46 AM
Post #6 of 22
(56202 views)
|
Re: [gopsi1234] Generate a C file from CSV file using perl
[In reply to]
|
Can't Post
|
|
Hi,
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.
printf "{%s, FLASH_CONFIGURATION_DEVICE(%s, %s, %s, %s, %s, %s, %s, %s)}\n", (split /, /)[1..3,5..9,0];
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".
#!/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:
#!/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:
#!/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:
#!/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:
__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)
|