Click here to Skip to main content
15,886,805 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to perl. I need a code to read a csv file and replace the 5th field by a value.

E.g. in the csv file if the 5th field is 'C' , then replace it as 1 else replace with 2.

The output file must be a different file.

I have written a code for it. Can anyone tell me if its correct or not.

#!/usr/bin/perl -w
$arg1=$ARGV[0];
$arg2=$ARGV[1];
open INP,"<" ,$arg1;
open OUT,">" ,$arg2;

while($line=<inp>)
{
    @arr=split(/,/,$line,5);
    if (arr[4] eq 'C')
    {
         arr[4]=1;
    }
    else
    {
         arr[4]=2;
    }
      $str=join(',',@arr);
     print OUT $str;
}

close INP;
close OUT;</inp>



Please help me someone.. Its very urgent.
If its not correct then please write an alternative code for it.

Thanks

Deepak
Posted
Updated 14-May-11 22:07pm
v2
Comments
Pong D. Panda 15-May-11 4:07am    
Test it, if it works, then it is.
deepakrout1987 16-May-11 5:31am    
If i would have the privilege of testing it, then wouldn't have asked for a code in a blog.
Sandeep Mewara 15-May-11 9:48am    
If its not correct then please write an alternative code for it.
Any one reason why should we?
deepakrout1987 16-May-11 5:34am    
I asked it just because i could not test it. Thats y asked in a blog.And i never asked u specifically to answer my question.

No Thanks
Sandeep Mewara 16-May-11 5:35am    
You still fail to learn what I wanted to say. And with this attitude, I doubt you will.

All the best and I hope someone writes an alternative code for you. Wait... only a day... may be soon someone. :thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900