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: [srenin] [Edit .CSV file] Add column: Edit Log



Zhris
Enthusiast

Nov 9, 2010, 1:38 AM


Views: 2145
Re: [srenin] [Edit .CSV file] Add column

Untested:


Code
#! /usr/bin/perl    
use strict;
use warnings;
use File::Basename;
use Text::CSV;

my $csv = Text::CSV->new();

my @files = glob("*.csv");

foreach my $file (@files) {
my $filename = $file; $filename =~ s/\.[^.]*$//;
open my $temp_fh, '>', "Temp.csv" or die "cannot open Temp.csv: $!";
open my $csv_fh, '<', "$file" or die "cannot open $file: $!";
while (my $line = <$csv_fh>) {
next if ($. == 1);
$csv->parse($line);
my @row = $csv->fields;
push (@row, $filename);
$csv->combine(@row);
my $newline = $csv->string();
print $temp_fh "$newline\n";
}
close $csv_fh;
close $temp_fh;
rename ("Temp.csv", "$file");
}


Chris


(This post was edited by Zhris on Nov 9, 2010, 9:29 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Nov 9, 2010, 1:40 AM
Post edited by Zhris (Enthusiast) on Nov 9, 2010, 1:45 AM
Post edited by Zhris (Enthusiast) on Nov 9, 2010, 1:52 AM
Post edited by Zhris (Enthusiast) on Nov 9, 2010, 9:28 PM
Post edited by Zhris (Enthusiast) on Nov 9, 2010, 9:29 PM
Post edited by Zhris (Enthusiast) on Nov 9, 2010, 9:29 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