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



Zhris
Enthusiast

Jun 23, 2014, 9:31 AM


Views: 59932
Re: [BillKSmith] Generate a C file from CSV file using perl

I concur with Bill. Remove the blank lines or adjust the code i.e. include "/^\s*$/ or" in the next condition or etc.

Heres two last examples which are much more forgiving with regards to the data format, but by no means flawless. In the first, I have broken up the core processes into separate functions, including basic validation. The second takes a regexp approach. There should be enough examples by now for you to fine tune to suit your particular dataset format.
_______________________________________________________________


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

while ( <DATA> )
{
if ( /^CHIP/ )
{
my @cols = row_to_cols( scalar <DATA> ); # fetch columns for row beneath CHIP row

print "invalid columns on row $.\n" and next unless validate_cols( @cols ); # if columns do not validate print error then move onto next row

printf "{%s, FLASH_CONFIGURATION_DEVICE(%s, %s, %s, %s, %s, %s, %s, %s)}\n", transform_cols( @cols ); # print formatted row using transformed columns
}
}

sub row_to_cols
{
my $row = shift;
return split /\s*,\s*/, $row; # split row up in columns using comma separator
}

sub validate_cols
{
my @cols = @_;
return @cols == 10 ? 1 : 0; # if number of columns equals 10, return true, otherwise return false
}

sub transform_cols
{
my @cols = @_;
$cols[-1] =~ s/\s+$//; # remove whitespace from end of last column.
return @cols[1..3,5..9,0]; # slice required columns and reorder
}

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


blah
CHIP xxx
0xBF, 0x2501, 32, xxx, 1, 3, 0, 0, 0, 20


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




foo

CHIP YYY
0xBF, 0x2501, 64, yyy, 1, 3, 0, 0, 0, 20
bar


CHIP zzz
0xBF, 0x2501, 128, zzz, 1, 3, 0, 0, 0, 20



Code
{0x2501, FLASH_CONFIGURATION_DEVICE(32, xxx, 3, 0, 0, 0, 20, 0xBF)} 
invalid columns on row 10
{0x2501, FLASH_CONFIGURATION_DEVICE(64, yyy, 3, 0, 0, 0, 20, 0xBF)}
{0x2501, FLASH_CONFIGURATION_DEVICE(128, zzz, 3, 0, 0, 0, 20, 0xBF)}

_______________________________________________________________


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

$/ = 'CHIP';

while ( <DATA> )
{
next if $. == 1 or $_ !~ /^.+(?:\n\s*)+(.+?)\s*\n/;

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

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


blah
CHIP xxx
0xBF, 0x2501, 32, xxx, 1, 3, 0, 0, 0, 20


CHIP YYY


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




foo

CHIP YYY
0xBF, 0x2501, 77, yyy, 1, 3, 0, 0, 0, 20
bar


CHIP zzz
0xBF, 0x2501, 128, zzz, 1, 3, 0, 0, 0, 20



Code
{0x2501, FLASH_CONFIGURATION_DEVICE(32, xxx, 3, 0, 0, 0, 20, 0xBF)} 
{0x2501, FLASH_CONFIGURATION_DEVICE(64, yyy, 3, 0, 0, 0, 0, 0xBF)}
{0x2501, FLASH_CONFIGURATION_DEVICE(77, yyy, 3, 0, 0, 0, 20, 0xBF)}
{0x2501, FLASH_CONFIGURATION_DEVICE(128, zzz, 3, 0, 0, 0, 20, 0xBF)}

_______________________________________________________________


Chris


(This post was edited by Zhris on Jun 23, 2014, 9:54 AM)


Edit Log:
Post edited by Zhris (Enthusiast) on Jun 23, 2014, 9:34 AM
Post edited by Zhris (Enthusiast) on Jun 23, 2014, 9:54 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