0
use Text::CSV;
$csv = Text::CSV->new;
open(HIGH, "+>Hardtest.csv") || die "Cannot open ticket $!\n"; #reads the high file
while(<HIGH>)
{
    print "Printing High Priority Tickets ...\n";
    sleep(1);
    print <HIGH>;
}
close(HIGH);

here is my code, i am trying to read a csv and write to it, however i cant seem to read the CSV file, help would be appreciated, thanks!

1
  • You aren't (yet) using the $csv that you created. Commented Apr 20, 2016 at 4:28

2 Answers 2

2

OK, lots of things here.

  1. Always use strict and use warnings.
  2. You're opening the CSV file write mode (append mode?). Don't do that, if you're just reading from it.
  3. Don't use || die, use or die.
  4. Finally, don't print <HIGH>, instead print $_.
Sign up to request clarification or add additional context in comments.

2 Comments

The reason to avoid printing <HIGH> is because the while already reads a line from the file, so you will only print every other line, correct?
Yes, the while loop is definitely reading the file, a line at a time (putting each line in $_), so printing <HIGH> could possible print the next line, or it might even try to print the whole file, or it might just print nothing, all of which are "bad".
0

I've modified your code a bit:

#!/usr/bin/perl -w
use strict;
use Text::CSV;

my $csv = Text::CSV->new;
open(HIGH, "+<Hardtest.csv") || die "Cannot open ticket $!\n"; #reads the high file
while(<HIGH>)
{
    print "Printing High Priority Tickets ...\n";
    print $_;
    sleep(1);
}
print HIGH "9,10,11,12\n";
close(HIGH);

Let me explain: 1. "+>" will open the file in read/write mode, BUT will also overwrite the existing file. Hence, In your code, while loop is never entered. I've changed that to "+<" which means read/write in append mode. 2. Second last statement, in above code, will append new content to the CSV file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.