Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: CSV file to C file conversion using perl

by sundialsvc4 (Abbot)
on Jun 19, 2014 at 13:26 UTC ( [id://1090462]=note: print w/replies, xml ) Need Help??


in reply to CSV file to C file conversion using perl

Okay, “not a code-writing service,” but here’s a bit of a leg-up since you’re new.   In general, your script will consist of something vaguely like this:   (extemporaneous coding ... might not compile ...)

use strict; use warnings; my $record; while (<INFILE>) { chomp; # remove newlines etc. in case it matters my ($tag, $value) = split(","); # implies "$_" if ($tag eq 'CHIP') { # note use of 'eq' not '==' $record = {}; $record->{'chip_id') = $value; } elsif ($tag eq 'END') { # PRINT '$record' IN NICE "C" FORMAT # THIS PART IS UP TO YOU ... } else { $record->{$tag} = $value; } }

The loop assembles, in $record, a hashref consisting of all of the values listed in the file, with an additional chip_id tag containing the value from the CHIP record.   The program “simply assumes” that the input-file is well-formed, with both CHIP and END records for each chip, and that all of the expected tags will be present for each chip ... a reasonable assumption in this case.

This logic is probably extremely similar to the logic that was used to create the file.

Now, carve out some time to spend with the perldocs that are tutorials, as well as things like perldoc -f split.   And, also spend considerable time using Super Search here.   Perl is actually well-documented, and there is a lot to it.   Welcome to the Monastery!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1090462]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found