How do I substitute with an evaluated expression in Perl?

前端 未结 3 786
梦谈多话
梦谈多话 2021-02-20 13:15

There\'s a file dummy.txt

The contents are:

 9/0/2010
 9/2/2010
 10/11/2010

I have to change the month portion (0,2,11) to +1, ie, (1,3

3条回答
  •  -上瘾入骨i
    2021-02-20 13:58

    How about this?

    $ cat date.txt 
    9/0/2010
    9/2/2010
    10/11/2010
    $ perl chdate.pl 
    9/1/2010
    9/3/2010
    10/12/2010
    $ cat chdate.pl 
    use strict;
    use warnings;
    
    open my $fp, '<', "date.txt" or die $!;
    
    while (<$fp>) {
        chomp;
        my @arr = split (/\//, $_);
        my $temp = $arr[1]+1;
        print "$arr[0]/$temp/$arr[2]\n";
    }
    
    close $fp;
    $ 
    

提交回复
热议问题