Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

remove a comma

by luupski (Initiate)
on Jun 20, 2018 at 17:22 UTC ( [id://1217031]=perlquestion: print w/replies, xml ) Need Help??

luupski has asked for the wisdom of the Perl Monks concerning the following question:

I have the following list in a notepad file called CUSTOMER.txt

--- this is CUSTOMER.txt (input) ---
0001 20000001 john CA
0002 30000002 neill WI
0003 40000003 joe GA
0004 50000004 will IL
0005 60000005 mike IN
0006 70000006 bill AK

I feed this into the script and get the following output which is placed in the CUSTOMER_NEW.txt file
All good for except 1 thing.
I want the last ' , ' (comma)to be removed before the closing bracket.
Tried several things after googling, but no success

So i was wandering if anybody has an idea how to add to the code given that will remove the last ' , '


--- this is CUSTOMER_NEW.txt (output) ---
SELECT CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION
FROM DB.CUSTOMER_DATA
WHERE (CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION) IN (
(0001,20000001,john,CA),
(0002,30000002,neill,WI),
(0003,40000003,joe,GA),
(0004,50000004,will,IL),
(0005,60000005,mike,IN),
(0006,70000006,bill,AK),
)

--- script used ---
sub customer {system ("notepad ./CUSTOMER.txt"); $dir="./"; $custom=$dir."CUSTOMER.txt"; $dir="./CUSTOMER_DIR/"; $ofile=$dir."CUSTOMER_NEW.txt"; open (IN,"$custom") || die "Cannot open CUSTOMER.txt!!!\n"; open (OUT,">$ofile") || die "Cannot open CUSTOMER_NEW.txt!!!\n"; print OUT "SELECT CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION FROM DB.CUSTOMER_DATA WHERE (CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION) IN (\n"; @CUST=<IN>;close IN; foreach $infile (@CUST){ chomp($infile); $i=0; @a=split(/\|/,$infile); @BAGO=$a[$i]; foreach $infile1 (@BAGO){ @b=split(/\s+/,$infile1); print OUT "($b[0],$b[1],$b[2],$b[3])\,\n"; $i++;}} print OUT ")\n"; system ("notepad ./CUSTOMER_DIR/CUSTOMER_NEW.txt"); close OUT; goto START; }

Replies are listed 'Best First'.
Re: remove a comma
by stevieb (Canon) on Jun 20, 2018 at 18:02 UTC

    Quick and dirty, check if you're at the end of file:

    use warnings; use strict; open my $fh, '<', 'file.txt' or die "can't open the damned file!: $!"; while (<$fh>){ chomp; if (! eof){ print "$_,\n"; } else { print "$_\n"; } }

    Where 'file.txt' contains:

    one two three

    Output:

    one, two, three
Re: remove a comma
by golux (Chaplain) on Jun 21, 2018 at 13:20 UTC
    Hi luupski,

    Another way to do this is with the join function, which lets you avoid appending the final comma in the first place.

    Read the data from your file, split it on whitespace, join each datum with commas, and surround the entire record with parentheses:

    foreach my $line (<$infh>) { if ($line =~ /$re_data/) { # Split on whitespace, surround with parens, join with commas push @data, "(" . join(",", split(/\s+/, $line)) . ")"; } }

    and finally, join the records with a comma followed by a newline. The last record won't get a comma (nor newline):

    # Simply 'join' all data; last record contains neither comma nor newli +ne. my $data = join(",\n", @data);

    Here's an entire working example:

    #!/usr/bin/perl use strict; use warnings; use feature qw( say ); use IO::File; ################## ## User-defined ## ################## my $input = 'in.txt'; # Input file my $output = 'out.txt'; # Output file my $re_data = qr/^[0-9]+/; # Detect lines containing records ################## ## Main program ## ################## # Read the input my $infh = IO::File->new($input) or die "Can't read '$input' ($!)"; my @data = ( ); foreach my $line (<$infh>) { if ($line =~ /$re_data/) { # Split on whitespace, surround with parens, join with commas push @data, "(" . join(",", split(/\s+/, $line)) . ")"; } } close($infh); # Simply 'join' all data; last record contains neither comma nor newli +ne. my $data = join(",\n", @data); # Create the output my $text = qq{ SELECT CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION FROM DB.CUSTOMER_DATA WHERE (CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION) IN ( $data ) }; $text =~ s/\n\s+/\n/g; # Remove indentation (for "looks") # Write the output my $outfh = IO::File->new; open($outfh, '>', $output) or die "Can't write '$output' ($!)"; print $outfh $text; close($outfh); say "Wrote '$output'";

    The output for which is:

    SELECT CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION FROM DB.CUSTOMER_DATA WHERE (CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION) IN ( (0001,20000001,john,CA), (0002,30000002,neill,WI), (0003,40000003,joe,GA), (0004,50000004,will,IL), (0005,60000005,mike,IN), (0006,70000006,bill,AK) )
    say  substr+lc crypt(qw $i3 SI$),4,5

      This works nice

      Had to ditch the +ne from the code and then all worked nicely


      Thanks once more

        Hi luupski,

        I'm not seeing it, but I guess the "+ne" (where "+" is colored red) is a continuation (word wrap) of the comment which ends with:

        # ... last record contains neither comma nor newline.

        You can avoid it by changing increasing the code wrap length in your User Settings or turning off code wrapping.

        Alternatively, you click on any [download] link; these take you to an unadulterated rendering of the text. (This is true for any node containing code samples).

        say  substr+lc crypt(qw $i3 SI$),4,5

      Thank you golux, going to give this a try

Re: remove a comma
by poj (Abbot) on Jun 20, 2018 at 19:33 UTC

    Which database is this, MySQL ? Don't the text values need to be in quotes ?

    IN ( (0001,20000001,'john','CA'),
    poj
Re: remove a comma
by mr_ron (Chaplain) on Jun 20, 2018 at 22:30 UTC

    You could also try fixing on a later step. Put the following small Perl script into chop-sql-comma.pl:

    my $sql = do {local $/; <>}; $sql =~ s/\( ([^()]+) \) , \n\)/($1)\n)/x; print $sql;

    Then run from a command prompt with perl "edit in place" mode:

    C:\Users\...>perl -i.bak chop-sql-comma.pl CUSTOMER_NEW.txt

    Update - after some more digging golfed to (again windows cmd prompt):

    perl -Mv5.14.0 -i.bak -e^ "local $/;print <> =~ s/\( ([^()]+) \) , \n\)/($1)\n)/xr"^ CUSTOMER_NEW.txt
    Ron

      Thnx Ron, will give this a run as well

Re: remove a comma
by luupski (Initiate) on Jun 20, 2018 at 20:25 UTC

    This is for an Oracle Database

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1217031]
Approved by NetWallah
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (1)
As of 2024-04-19 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found