Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Perl script to read CSV

Avatar of ycgtech
ycgtech asked on
Perl
4 Comments1 Solution582 ViewsLast Modified:
Hi, I am trying to write a script that will read a csv and then write that inormation to another csv (eventually I will add options to sort, and pull out specific data), but right now I am working on the basics.  The script I have so far is below.  Usage is: $ perlscript.pl file1.csv file2.csv.  I am having two issues (that are perhaps related to the same thing).  Firstly, file2.csv is created and the information is properly formatted, however there is one extra set of "filename, path, etc" at the bottom of the file with no data.  Secondly, although the file is created, I receive the following error: Use of uninitialized value $path in concatenation (.) or string at readfile3.pl line 26, <CSV> line 7.  I received that error for $path $size $created $modified $accessed $deleted, but not $filename.  The test csv I am using has 6 rows (the first one being the header which I removed in the script).

Unrelated, the output that the script gives me shows a space between each character.  How do I remove that?

Code:

my $file = $ARGV[0];
my $newfile = $ARGV[1];

open (CSV, $file) or die $!;
open (CSV2, ">", $newfile) or die $!;

while (<CSV>) {
      next if ($. == 1);
      chomp;
      my ($filename, $path, $size, $created, $modified, $accessed, $deleted, $junk1, $junk2) = split ("\t");
      print CSV2 ("Filename: $filename\n Path: $path\n Size: $size\n Created: $created\n Modified: $modified\n Accessed: $accessed\n Deleted: $deleted\n");
      print CSV2 "------------------------------\n";
}

close (CSV);
close (CSV2);

Thanks,