Solved

Writing at the begining of the .csv file

Posted on 2007-03-30
10
193 Views
Last Modified: 2010-03-05
I have a .csv file in following format:

111111111,F_NAME1 ,L_NAME1,ADDR1,CITY1,MS,12121,787878787,1234567,1234567890
222222222,F_NAME2 ,L_NAME2,ADDR2,CITY2,TN,98765,121212121,98767890,2020202020
333333333,F_NAME3 ,L_NAME3,ADDR3,CITY3,TN,10101,454545454,56565656,1010101010
444444444,F_NAME4 ,L_NAME4,ADDR4,CITY4,TN,12345,987612234,12312312,8989898989

Each row represents one record. I need to write some $result at the begining of each row as one more field/column.
The value of $result will be different for each row, so I know I need to do something like - foreach to go to the next row
but don't know how to do it.

Please help!
0
Comment
Question by:MPJD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18824703
open(CSV,">path_to_your_file");
while ($line = <CSV>)
{
$line = $result.$line;
print CSV $line;
}
but i did not sure that symbol '>' is for read write or for write only
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18824711
I will check it
0
 
LVL 6

Expert Comment

by:nitinsawhney
ID: 18824750
Here is another version

open(CSVIN,"<path_to_your_file"); # Read original file
open(CSVOUT,">path_to_output_file"); # Write new file
while ($line = <CSVIN>)
{
$line = $result.$line;
print CSVOUT $line;
}

Also, if you would like keep the same name as the original file then in the end use system to move the newly created file to old one.

system('mv path_to_output_file path_to_your_file')

Cheers
Nitin
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18824754
Ok
the symbol '+<' is for read/write
open CSV, "+<path_to_your_file";
while ($line = <CSV>)
{
$line = $result.$line;
print CSV $line;
}
close CSV
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18824798
if you used nitinsawhney's option dont forget to close the file descriptors

open CSVIN,"<path_to_your_file"; # Read original file
open CSVOUT,">path_to_output_file"; # Write new file
while ($line = <CSVIN>)
{
$line = $result.$line;
print CSVOUT $line;
}
close CSVIN;
close CSVOUT;
system('mv path_to_output_file path_to_your_file')
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 18826956
This:

open(CSV,">path_to_your_file");
while ($line = <CSV>)
{
$line = $result.$line;
print CSV $line;
}

wil clobber the existing file -- don't try this!  nitinsawhney's second version id OK.  Also, Perl will close the filehandles for you; there's no need to do so explicitly (in this case).  There's no harm if you do it, though.
0
 
LVL 1

Author Comment

by:MPJD
ID: 18837216
thanks everybody!

but when I am doing -
$line=$result.$line,

$result is replacing the 1st column from the file like -

if the file has $line = c1,c2,c3 then after I do
$line = $result.$line, it shows me -

$result,c2,c3

I don't want to replace c1 by $result, I want to write $result before c1 so that it should look like -
$result,c1,c2,c3

I appreciate your help!
0
 
LVL 1

Author Comment

by:MPJD
ID: 18837264
SORRY!! I take back my words,
 its indeed working when I put $result.','.$line
So please ignore my last comment, I am so stupid!
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18837271
No problem at all
0
 
LVL 4

Accepted Solution

by:
mukhtar2t earned 500 total points
ID: 18837299
I actualy missed the comma as you said the code must be :
open CSV, "+<path_to_your_file";
while ($line = <CSV>)
{
$line = $result.','.$line;
print CSV $line;
}
close CSV;

same as you said on your last post

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question