CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate: Re: [pradeep1982] Write the data by preserving quote characters: Edit Log

Veteran / Moderator

Dec 9, 2014, 6:00 PM

Views: 6082
Re: [pradeep1982] Write the data by preserving quote characters

Your code has multiple problems, some are minor but some aren't.

$csv->getline($fh1) returns an array ref, not an array.

Your splice statement and resulting array assignment will need to be adjusted to use array refs.

Your use of $firstLine is unnecessary. Just read 1 line in void context before the loop.

When you print the array ref, you need to pass the entire thing, not just the first element.

I used perltidy to clean up the formatting, but it still needs some work.

There are additional issues I didn't address, the ones I pointed are the most important.

use Getopt::Long; 
use File::Copy;
use Text::CSV;

my $filename = '/mydir/file.txt';
my $filename2 = '/mydir/file_formatted.csv';

unlink $filename;

for ( my $i = 1 ; $i <= 2 ; $i++ ) {
my $resp = $SSO->get($WS_URL);
my $data = $resp->content();
last if ! $data;
if ($data) {
my $csv = Text::CSV->new(
binary => 1, # Allow special character. Always set this
auto_diag => 1, # Report irregularities immediately
sep_char => ',', #Separatrp
open( my $fh1, "<:encoding(utf8)", \$data ) or die "$!";
open( my $fh2, '>>', $filename )
or die "Could not open file '$filename' $!";

while ( my $row = $csv->getline($fh1) ) {

# print qq("$row->[0]|$row->[4]\n)";
my $newrow = splice( @$row, 0, 3 );
print "@$newrow\n";
$csv->print( $fh2, $newrow );

close $fh2;
close $fh1;


This line may need to be changed depending on what results you're expecting. I'm not sure what you intended.

my $newrow = splice( @$row, 0, 3 );

(This post was edited by FishMonger on Dec 9, 2014, 6:05 PM)

Edit Log:
Post edited by FishMonger (Veteran) on Dec 9, 2014, 6:05 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