Help. General Expression with a specific (right align) location.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nk215
    New Member
    • Nov 2006
    • 3

    Help. General Expression with a specific (right align) location.

    Hello

    I have a regular expression question.

    Here's what I want to do

    change
    TEST RADIUS 123.254
    to
    TEST RADIUS 3.25

    123456789012345 678901234567890

    The key is to keep the number start at the specific column (right align). The numbers can have up to 3 decimal places. It must be at a specific location from the right. In this example, the number start from column 26 - Right Align. The text start at the second column - Left Align.

    Is there away for Perl to do this?

    Becuuse of the font/format. The numbers done line up correction in the post but you get the idea.

    Thanks
    -Khanh
  • docsnyder
    New Member
    • Dec 2006
    • 88

    #2
    Hi Khanh!

    Please, can you email the example to the following address: docsnyder@onlin e.de

    Send the email as ASCII with the correct alignment (original text and desired text).

    Greetz, docsnyder

    P.S. Is it right, that the text should be changed to "TEST RADIUS 3.25" (i.e. changing the number from 123.254 to 3.25)?

    Originally posted by nk215
    Hello

    I have a regular expression question.

    Here's what I want to do

    change
    TEST RADIUS 123.254
    to
    TEST RADIUS 3.25

    123456789012345 678901234567890

    The key is to keep the number start at the specific column (right align). The numbers can have up to 3 decimal places. It must be at a specific location from the right. In this example, the number start from column 26 - Right Align. The text start at the second column - Left Align.

    Is there away for Perl to do this?

    Becuuse of the font/format. The numbers done line up correction in the post but you get the idea.

    Thanks
    -Khanh

    Comment

    • nk215
      New Member
      • Nov 2006
      • 3

      #3
      Originally posted by docsnyder
      Hi Khanh!

      Please, can you email the example to the following address: docsnyder@onlin e.de

      Send the email as ASCII with the correct alignment (original text and desired text).

      Greetz, docsnyder

      P.S. Is it right, that the text should be changed to "TEST RADIUS 3.25" (i.e. changing the number from 123.254 to 3.25)?

      Thank you. The email is on the way. Here's my Logic and my best try

      I was trying to read in the entire file and perform a general expression search and replace. Needless to say, it works but the number is is the wrong column since $elem can be rather random in length. $elem is read from another file previously.

      Here's my logic.

      1) Read in the first file (base.txt in this example).
      2) Read in the next file (input_table.tx t) which is a table with about 400 rows and 3 columns.

      3) For each row in the second file, create a new txt file by search and replace the specific string in the base.txt with the new values from the input_table.txt . The number has to end at a specific column. The text has to start at column 2.

      Please help
      Thanks
      -Khanh


      #
      # perl test.pl < input_table.txt
      #

      open (IN, "<base.txt" );


      while (<STDIN>) {
      chomp;
      s/^\s+//; #replace beginning space with nothing
      @val = split (/\s+/);

      $elem = $val[0]; # Element
      $radius = $val[1]; # Radius
      $spacing = $val[2]; # Spacing


      open (OUT, "+>cdi_$elem.tx t");

      @file = <IN>;

      seek IN,0,0;

      foreach $file (@file){
      $file =~ s/ TEST RADIUS......... ..../ TEST RADIUS $elem/g;
      # This is where I have a problem with.
      #I don't know enough about seach and replace.

      print OUT $file ;

      }

      # Other operations here.

      };

      close IN;
      close OUT;

      Comment

      • docsnyder
        New Member
        • Dec 2006
        • 88

        #4
        Hi Khanh!

        Here is my solution:

        Code:
        $_ = "TEST RADIUS 123.254";
        
        s/^\s+//; #replace beginning space with nothing
        @val = split (/\s+/);
        
        $elem      = $val[0]; # Element
        $radius    = $val[1]; # Radius
        $spacing   = $val[2]; # Spacing
        $newRadius = 3.25;
        
        $col        = 26;
        $lenElem    = length($elem);
        $lenRadius  = length($radius);
        $lenSpacing = length($newRadius);
        
        $result1 = sprintf("%*s %*s %*s", $lenElem, $elem, $lenRadius, $radius, $col-$lenElem-$lenRadius-2, $newRadius);
        $result2 = sprintf("%*s %*s %*s", $lenElem, $elem, $lenRadius, $radius, $col-$lenElem-$lenRadius+$lenSpacing-3, $newRadius);
        
        printf("         1         2         \n");
        printf("12345678901234567890123456789\n");
        printf("$result1\n");
        printf("$result2\n");
        I don't know, whether the number should be right aligned at it's first digit ($result2) or at it's last digit ($result1). Use the one you like ...

        Greetz, Doc

        Originally posted by nk215
        Thank you. The email is on the way. Here's my Logic and my best try

        I was trying to read in the entire file and perform a general expression search and replace. Needless to say, it works but the number is is the wrong column since $elem can be rather random in length. $elem is read from another file previously.

        Here's my logic.

        1) Read in the first file (base.txt in this example).
        2) Read in the next file (input_table.tx t) which is a table with about 400 rows and 3 columns.

        3) For each row in the second file, create a new txt file by search and replace the specific string in the base.txt with the new values from the input_table.txt . The number has to end at a specific column. The text has to start at column 2.

        Please help
        Thanks
        -Khanh


        #
        # perl test.pl < input_table.txt
        #

        open (IN, "<base.txt" );


        while (<STDIN>) {
        chomp;
        s/^\s+//; #replace beginning space with nothing
        @val = split (/\s+/);

        $elem = $val[0]; # Element
        $radius = $val[1]; # Radius
        $spacing = $val[2]; # Spacing


        open (OUT, "+>cdi_$elem.tx t");

        @file = <IN>;

        seek IN,0,0;

        foreach $file (@file){
        $file =~ s/ TEST RADIUS......... ..../ TEST RADIUS $elem/g;
        # This is where I have a problem with.
        #I don't know enough about seach and replace.

        print OUT $file ;

        }

        # Other operations here.

        };

        close IN;
        close OUT;

        Comment

        Working...