John W. Krahn wrote:
> Goke Aruna wrote:
>>i want to process the attached file and i want to replace the $data[1] with
>>equivalent month is strings.
>>
>>i have this code but it giving me errors... i only try the january case at
>>least.
>>
>>#!c:/perl/bin/perl
>>use warnings ;
>>use strict ;
>>use POSIX 'strftime';
>>
>>my $file = "c:/Perl/test.csv" ;
>>
>>{ local ($\, $,) = ("\n", ', ') ;
>>  my $sum ;
>>  open my $fh, '<', $file or die "open '$file': $!" ;
>>
>>  while (<$fh>)
>>  {
>>    s/\s+$// ;  # chomps-and-more
>>    /^$/ and die "$file, line $. is empty\n" ;
>>    my @data = (split /,/)[0, 1, 2, 3, 4, 5, 6] ;
>>    # $data[0] or next ;  # skips
>>  #  my $cnt_ans = 0;
>>    if($data[1] == 1)
>>         {
>>           while($data[1] == 1)
>>           {$data[1]=~ tr/1/January/;}
>>     } elsif ($data[1] == 2){
>>        replace all occurence of 2 with February
>>      } elsif ($data[1] == 3){
>>        replace all occurence of 3 with March
>>      } elsif ($data[1] == 5){
>>       replace all occurence of 5 with May
>>     } elsif ($data[1] == 6){
>>     replace all occurence of 6 with June
>>     } elsif ($data[1] == 7){
>>     replace all occurence of 7 with July
>>     } elsif ($data[1] == 8){
>>      replace all occurence of 8 with August
>>     }  elsif ($data[1] == 9){
>>        replace all occurence of 9 with September
>>      } elsif ($data[1] == 10){
>>        replace all occurence of 10 with Octomber
>>      } elsif ($data[1] == 11){
>>        replace all occurence of 11 with November in the column
>>      } elsif ($data[1] == 12){
>>        replace all occurence of 12 with December
>>      }
>>}
> 
> #!c:/perl/bin/perl
> use warnings ;
> use strict ;
> 
> my $file = 'c:/Perl/test.csv' ;
> 
> { local ( $\, $, ) = ( "\n", ', ' ) ;
> 
>   my %conversion = qw(
>     January    1
>     February   2
>     March      3
>     April      4
>     May        5
>     June       6
>     July       7
>     August     8
>     September  9
>     October   10
>     November  11
>     December  12
>     );

Oops, that should be:

  my %conversion = qw(
     1 January
     2 February
     3 March
     4 April
     5 May
     6 June
     7 July
     8 August
     9 September
    10 October
    11 November
    12 December
    );

>   open my $fh, '<', $file or die "open '$file': $!" ;
> 
>   while ( <$fh> ) {
>     s/(?<=^\d{8},)(\d+)(?=,)/exists $conversion{$1} ? $conversion{$1} : $1/e;
>     print;
>     }
>   }


John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to