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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [BigRedEO] Parsing CSV file - Add Concatenated field/sort: Edit Log



FishMonger
Veteran / Moderator

Mar 24, 2016, 7:01 AM


Views: 38799
Re: [BigRedEO] Parsing CSV file - Add Concatenated field/sort

The output was put all on 1 line because your print statement is missing the \n new line character.

Here's a slightly adjusted/improved version.

Code
#!/usr/bin/perl/  

use strict;
use warnings;

my $filename = '/swpkg/shared/batch_processing/mistints/test.csv';
$filename = 'test.csv';

open my $FH, $filename
or die "Could not read from $filename <$!>, program halting.";

# Read the header line.
my $line = <$FH>;

# Display the header, just to check things are working.
print $line, $/;

# Read the lines one by one.
while($line = <$FH>) {

# split the fields, concatenate the first three fields,
# and add it to the beginning of each line in the file
chomp($line);
my @fields = split(/,/, $line);
unshift @fields, join '_', @fields[0..2];
print $fields[0], $/;
}

close $FH;
exit;


Which outputs:

Quote
c:\test>BigRedEO.pl
STORE_NBR,CONTROL_NBR,LINE_NBR,SALES_NBR,QTY_MISTINT,REASON_CODE,MISTINT_COMM,SZ_CDE,TINTER_MODEL,TINTER_SERL_NBR,SPECTRO_MODEL,SPECTRO_SERL_NBR,EMP_NBR,TRAN_DATE,TRAN_TIME,CDS_ADL_FLD,PROD_NBR,PALETTE,COLOR_ID,INIT_TRAN_DATE,GALLONS_MISTINTED,UPDATE_EMP_NBR,UPDATE_TRAN_DATE,GALLONS,FORM_SOURCE,UPDATE_TRAN_TIME,SOURCE_IND,CANCEL_DATE,COLOR_TYPE,CANCEL_EMP_NBR,NEED_EXTRACTED,MISTINT_MQ_XTR,DATA_SOURCE,GUID,QUEUE_NAME,BROKER_NAME,MESSAGE_ID,PUT_TIME,CREATED_TS

1334_53927_1
2525_67087_1
1350_163689_1



(This post was edited by FishMonger on Mar 24, 2016, 7:05 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Mar 24, 2016, 7:05 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives