On Sat, Feb 7, 2009 at 08:45, Soham Das <[email protected]> wrote:
> Hi All,
>
> I am a noob in Perl and hence would like some help to what I am sure is a
> very easy problem.
>
> I have got a text file in csv format
> The format is:
> <TICKER>,<DATE>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOLUME>,<OI>
>
> Now my objective is to change the format of the date, and rename the whole
> file as a .csv
>
> So, my strategy is:
> I want to read the content between the first and second comma, take it in a
> variable and do the slicing and dicing and write it back.
>
> Because I need some real life practice in REGEX, how do you suggest I read
> the contents between the first and the second comma?
snip
This isn't a job for a regex; it is a job for split:
my @record = split ",", $record;
$record[1] =~ s{(..)/(..)/(....)}{$3$1$2}
or die "line $. has an invalid date format";
print join ",", @record;
You could say
$record =~ s{(.*?),(..)/(..)/(....),}{$1,$4$2$3,}
or die "line $. has an invalid date format";
print $record;
but the next person to maintain your code may be a little upset at
you, especially in the more complicated versions of this type of
substitution.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/