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

Jul 2, 2014, 7:44 AM


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

From private message:


Quote
Hey Chris

Its working fine now. Below is the code. Can it be better than this. Here i have separated the header first and then parsed the other values. Is it possible to seperate header also and putting the entire thing under 1 while loop. Right now, m using 2. Also what else can be improved?



Code
  
#!/usr/bin/perl -w

# use strict;
# use warnings;
open (DATA, "<input.csv") or die "Can't open flash config repository: $!";

my @headers;
while(<DATA>){
next if m/^\s*$/ or /\A#/; # skip blank lines or comments
s/\s+$//; # remove any whitespace from the end of the line.
@headers=split(/\s*,\s*/); # split the header names.
last; # break out of the loop: the header was found
}
my @records;
while(<DATA>){
next if m/^\s*$/ or /\A#/; # skip blank lines or comments
my @fields = split(/\s*,\s*/);
my %hash;
@hash{@headers} = @fields; # use hash slice to assign fields to headers
push @records, \%hash; # add this hashref to records
}
for my $row (@records)
{
printf "%s, %s\n", @{$row}{qw/vendorid memid/};
}

________________________________________________________________

1) Don't comment out using strict and warnings. As stated previously, if they raise any issues, you should fix rather than ignore. http://www.perlmonks.org/?node=use%20warnings%20vs.%20perl%20-w

2) The modern approach to opening a file is to use the 3 way form with a lexical variable filehandle, over a bareword filehandle.

3) In your previous code snippet, you used the 'if ( $. == 1 ) {' condition to handle the header, is there any reason you can't do this, i.e. it assumes the header is on line 1. If you aren't already aware, $. contains the line number you are currently reading from the filehandle, therefore the above condition fundamentally checks if we are on line 1. In my version below I assume the first valid line reached is the header line. I simply check if @headers contains any values, if it doesn't then we must be on the header line, we assign it some values, then other iterations @headers will contain values, therefore assumed records.

4) Although not necessary, its good practice to close the filehandle once you have finished with it.

5) Although not implemented in my version below, you don't actually have to store the records in @records, you might as well print directly i.e.:


Code
push @records, \%hash; 

printf "%s, %s\n", @hash{qw/vendorid memid/};


Here is my version of your code:


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

my @headers;
my @records;

open my $fh, '<', 'input.csv' or die "Can't open flash config repository: $!";

while (my $line = <$fh>)
{
next if ( $line =~ m/^\s*$/ or $line =~ m/\A#/ ); # skip blank lines or comments

$line =~ s/\s+$//; # remove any whitespace from the end of the line.

my @fields = split /\s*,\s*/, $line; # split line up into individual fields.

if (@headers) # headers have already been assigned.
{
my %hash;
@hash{@headers} = @fields; # use hash slice to assign fields to headers
push @records, \%hash; # add this hashref to records
}
else # headers have not yet been assigned.
{
@headers = @fields; # assign fields to headers.
}
}

close $fh;

for my $entry (@records)
{
printf "%s, %s\n", @$entry{qw/vendorid memid/};
}


input.csv:

Code
 

vendorid,memid,size,devid,cmd,addr,conf_width,dummy,mode,rate,devicename




0xBF,0x5501,32,est,1,3,0,0,0,20,xxx
#comment
0xBF,0x5502,64,est,1,3,0,0,0,20,yyy
0xBF,0x5503,128,est,1,3,0,0,0,20,zzz


Regards,

Chris


(This post was edited by Zhris on Jul 2, 2014, 8:07 AM)


Edit Log:
Post edited by Zhris (Enthusiast) on Jul 2, 2014, 7:46 AM
Post edited by Zhris (Enthusiast) on Jul 2, 2014, 7:48 AM
Post edited by Zhris (Enthusiast) on Jul 2, 2014, 7:50 AM
Post edited by Zhris (Enthusiast) on Jul 2, 2014, 7:56 AM
Post edited by Zhris (Enthusiast) on Jul 2, 2014, 7:59 AM
Post edited by Zhris (Enthusiast) on Jul 2, 2014, 8:06 AM
Post edited by Zhris (Enthusiast) on Jul 2, 2014, 8:07 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