Reading & updating certain variable in file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • indra061
    New Member
    • Jan 2007
    • 2

    Reading & updating certain variable in file

    Hi all,

    I am newbie of perl. The story is, i need to read and update one variable from text file (WARPFREQ = 0.88). The value of WARPFREQ will be inceremented by 0.2 up to 1.2. Everytime the value changed, it will be saved to the same line and file. The problem is the line of (WARPFREQ = 0.88) was deleted and the updated line from the process (WARPFREQ = 1.28) was inserted without shifting the below line. Additionally, the incremental process didnot work too. It was supposed to be 1.08 but it was considered as string not a scalar.

    I would appreciate any help.

    Regards,

    indra

    filemod.pl

    #!/usr/bin/perl
    $file = "confVTLN";
    $tmp = "confVTLN.t mp";

    my $file = shift;
    my $tmp = $file . ".tmp";

    my $disclaimer = "RAWENERGY" ;

    open(OLD, "< confVTLN") or die "open $file: $!";
    open(NEW, "> confVTLN.tmp") or die "open $tmp: $!";

    while (my $line = <OLD>) {
    print NEW $line or die "print $tmp: $!";

    if ($line =~ m/$disclaimer/) {
    $line = <OLD>;
    ($param,$val)=s plit(/ *= */,$line);
    while ($val<=1.2){
    $val+=0.2;
    $line = join('=', $param,$val);
    }
    print NEW $line;
    }

    }
    close(OLD) or die "close $file: $!";
    close(NEW) or die "close $tmp: $!";

    unlink("confVTL N") or die "unlink $file: $!";
    rename("confVTL N.tmp", "confVTLN") or die "can't rename $tmp to $file: $!";

    1;


    confVTLN file:

    ESCALE = 1.0

    TRACE = 0

    RAWENERGY = F
    WARPFREQ = 0.88
    WARPLCUTOFF = 100
    WARPUCUTOFF = 4000
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Not exactly sure I understand what all you are trying to do, but using this code:

    Code:
    my $file = 'confVTLN.txt';
    {
       local @ARGV = ($file); 
       local $^I = '.bac'; #creates a backup of the original file with a .bac extension
       while( <> ){ 
          if(/^(WARPFREQ = )(\d+.?\d*)/) {
             my $n = $2;
             $n += .2;  
             print "$1$n$/"; 
          } 
          else { 
             print; 
          } 
       }
    }
    print "finished";
    on this file:

    Code:
    ESCALE = 1.0
    
    TRACE = 0
    
    RAWENERGY = F
    WARPFREQ = 0.88
    WARPLCUTOFF = 100
    WARPUCUTOFF = 4000
    produces this output:

    Code:
    ESCALE = 1.0
    
    TRACE = 0
    
    RAWENERGY = F
    WARPFREQ = 1.08
    WARPLCUTOFF = 100
    WARPUCUTOFF = 4000

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      oops, forgot to check for the max value of 1.2, here it s added to the above code:


      Code:
      my $file = 'confVTLN.txt';
      {
         local @ARGV = ($file); 
         local $^I = '.bac'; #creates a backup of the original file with a .bac extension
         while( <> ){ 
            if(/^(WARPFREQ = )(\d+.?\d*)/) {
               my $n = $2;
               $n += .2;  
               [B]$n = ($n > 1.2)  ? 1.2 : $n;[/B]
               print "$1$n$/"; 
            } 
            else { 
               print; 
            } 
         }
      }
      print "finished";

      Comment

      • indra061
        New Member
        • Jan 2007
        • 2

        #4
        Thanks Kevin.
        Your code worked fine.

        Originally posted by KevinADC
        oops, forgot to check for the max value of 1.2, here it s added to the above code:


        Code:
        my $file = 'confVTLN.txt';
        {
           local @ARGV = ($file); 
           local $^I = '.bac'; #creates a backup of the original file with a .bac extension
           while( <> ){ 
              if(/^(WARPFREQ = )(\d+.?\d*)/) {
                 my $n = $2;
                 $n += .2;  
                 [B]$n = ($n > 1.2)  ? 1.2 : $n;[/B]
                 print "$1$n$/"; 
              } 
              else { 
                 print; 
              } 
           }
        }
        print "finished";

        Comment

        Working...