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 19, 2014, 2:28 PM


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

Hi,

Here is possibly something to get you going. You describe your input as a CSV but your example and description of the data does not correlate with this format, I have assumed your input is chunks of key value pairs. It reads each CHIP .. END chunk of your input data, parses it into a hash, then outputs it in your desired format. I'm sure you will figure out how to read from and write to actual files.


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

my $hash = { };

while ( <DATA> )
{
if ( /CHIP/ .. /END/ )
{
#chomp;

if ( /END/ )
{
printf "{%s, FLASH_CONFIGURATION_DEVICE(%s, %s, %s, %s, %s, %s, %s, %s, %s)}\n",
@{$hash}{qw/memid size devid cmd addr mode dummy conf_wid rate vendorid/};

$hash = { };
}
else
{
my ( $key, $val ) = $_ =~ /(\S+)\s*,\s*(\S+)/;

$hash->{$key} = $val;
}
}
}

__DATA__
CHIP,xxxx
vendorid,0xBF
memid,0x2501
size,256
devid,yyy
cmd,1
addr,3
mode,0
dummy,0
conf_wid,0
rate, 20
END

CHIP,xxxx
vendorid,0xBF
memid,0x2501
size,2564
devid,yyy
cmd,1
addr,3
mode,0
dummy,0
conf_wid,0
rate, 20
END


Hope this helps,

Chris


(This post was edited by Zhris on Jun 19, 2014, 2:46 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Jun 19, 2014, 2:34 PM
Post edited by Zhris (Enthusiast) on Jun 19, 2014, 2:46 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