gopsi1234 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on CSV file to C file conversion using perl

Replies are listed 'Best First'.
Re: CSV file to C file conversion using perl
by Corion (Patriarch) on Jun 19, 2014 at 13:10 UTC

    This is not a code writing service. While we will try to help you with the code you have already written and with the problems you encounter with it, we won't do your work for you.

    Please show what code you have already written and where you encounter problems with it. Show us what output you expect, and what output you get instead.

    I recommend looking at Text::CSV_XS for reading the CSV file, and at the sprintf function for creating the macro line.

    If the C code is to be more complex, have a look at Template or HTML::Template for creating more complex output.

Re: CSV file to C file conversion using perl
by locked_user sundialsvc4 (Abbot) on Jun 19, 2014 at 13:26 UTC

    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!

Re: CSV file to C file conversion using perl
by Anonymous Monk on Jun 19, 2014 at 17:37 UTC
    Something like this might work (won't necessarily work, but should give you an idea)
    use strict; use warnings; $/ = 'END'; # see perldoc -v '$/' open my $infile, '<', '/NAME/OF/FILE.csv' or die; open my $outfile, '>', '/NAME/OF/FILE.c' or die; while ($infile) { s/\A \s+ | \s+ \z//gx; # delete leading and trailing whitespace my %record = split /\n|,/; # this is brittle, tweak as necessary # now do something with the record, for example... print $outfile "${record{memid}, FLASH(${record{size}})" # etc }
      Damn, I forgot. You must delete END from the end.  s/\A \s+ | \s* (END)? \z//gx

      Hi Monk

      Thanks for the initial help. I have started writing the perl script based on your suggestion and will get back to you once I am done and if I face any issue i will post it here.

      Hi Monk

      Thanks for the initial help. I have started writing the perl script based on your suggestion and will get back to you once I am done and if I face any issue i will post it here.

      Anonymous Brother, prithee, what is FLASH() ??

        FLASH_CONFIGURATION_DEVICE from the original post :)