Results 1 to 6 of 6

Thread: [perl] converting a string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Qt products
    Qt3
    Platforms
    Windows
    Thanks
    53

    Default Re: [perl] converting a string

    Qt Code:
    1. my $v1 =0;
    2. my $v2=0;
    3. ........................................
    4. if ($_ =~ m/Content:/) {
    5. $v1 = # I need to extract Value1....
    6. $v2 = # I need to extract Value2....
    7. if ( $v2 < 1) { //this is ok
    8. ......................
    9. }
    10. //the file is:
    11. SomeString SomeString
    12. Content:
    13. # blank line
    14. SomeString SomeString SomeString SomeString Value1 Value2
    15. SomeString SomeString Value1 Value2
    16. SomeString SomeString SomeString Value1 Value2
    17. # blank line
    18. SomeString
    19. SomeString SomeString
    20. .........................
    To copy to clipboard, switch view to plain text mode 
    As you can see there are some lines that end with two numbers. Is it possible detect only that line and extract the last two values?

    thanks,
    Regards

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: [perl] converting a string

    Qt Code:
    1. my $v1 =0;
    2. my $v2=0;
    3. # match all lines that end with two numbers
    4. # (note: only unsigned integers are matched, not floats like 2.3, -3.4, 2e-3 ...!)
    5. if ($_ =~ m/.*/\s+(\d+)\s+(\d+)\s*$/)
    6. {
    7. $v1 = $1;
    8. $v2 = $2;
    9.  
    10. if ( $v2 < 1) {
    11. ...
    To copy to clipboard, switch view to plain text mode 

    If you need to match not only integers the you need to replace the (\d+) by more complicated regexs for whatever numbers you need to match. See, e.g. http://docstore.mik.ua/orelly/perl/cookbook/ch02_02.htm

    HTH

Similar Threads

  1. How to use PERL script in Qt
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 21:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.