Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Modifying an existing Perl script to ask for input and output filenames and also remove double quotes for outfile file

by poj (Abbot)
on Feb 01, 2018 at 18:56 UTC ( [id://1208273]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Modifying an existing Perl script to ask for input and output filenames and also remove double quotes for outfile file
in thread Modifying an existing Perl script to ask for input and output filenames and also remove double quotes for outfile file

Try

#!/usr/bin/perl use strict; use Text::CSV; my @hdr = qw( DeviceID PosTxnID MobileNumber Timestamp PosID PosUserID ShopID Prodcode ProdDescription ProdDept ProdGroup1 ProdGroup2 Qty Value); my @e14 = ('','','','','','','','','','','','','',''); my $spacer = 0; # grab user input.. print "Enter the name of the file to read: "; my $filetoread = <STDIN>; chomp ($filetoread); print "Enter the name of the file to write: "; my $filetowrite = <STDIN>; chomp ($filetowrite); open my $fh_read, '<', $filetoread or die "Unable to read [$filetoread] : $!"; my $csvi = Text::CSV->new( { binary=>1 } ); open my $fh_write, '>', $filetowrite or die "Unable to write [$filetowrite] : $!"; my $csvo = Text::CSV->new( { binary=>1, eol=>$/, quote_space => 0} ); my $ar = $csvi->getline($fh_read); s/ //g for @$ar; $csvi->column_names($ar); $csvo->column_names(@hdr); $csvo->print($fh_write,\@hdr); my ($code,$desc); print "Reading file $filetoread\n"; while (my $hri = $csvi->getline_hr($fh_read)) { if ($hri->{InvDate}) { my ($dd,$mm,$yyyy) = split "/",$hri->{InvDate}; my $hro = { DeviceID => $hri->{CustCode}, PosTxnID => $hri->{InvNum}, MobileNumber => '', Timestamp => $yyyy.'-'.$mm.'-'.$dd.' 00:00', PosID => '', PosUserID => '', ShopID => '2345', Prodcode => $code, ProdDescription => $desc, ProdDept => '', ProdGroup1=> '', ProdGroup2=> '', Qty => $hri->{Qty}, Value => $hri->{NetAmt} }; if ($spacer) { $csvo->print($fh_write,\@e14); $spacer = 0; } $csvo->print_hr($fh_write,$hro); } elsif ($hri->{CustCode}) { ($code,$desc) = split ' - # ',$hri->{CustCode}; } elsif ($hri->{LineDisc}) { $spacer = 0; } } close $fh_read; close $fh_write; print "Created file $filetowrite\n";
poj
  • Comment on Re^3: Modifying an existing Perl script to ask for input and output filenames and also remove double quotes for outfile file
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1208273]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-24 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found