Results 1 to 6 of 6

Thread: [perl] converting a string

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

    Default [perl] converting a string

    Hello,
    i have a problem with perl. Se below.
    Qt Code:
    1. my $v =0;
    2. ........................................
    3. if ($_ =~ m/value = (\S+)/) {
    4. $v = $1; // $1 take the right value eg. "e-33"
    5. if ( $v < 1) { # here the error
    6. ......................
    7. }
    8. //the file is:
    9. ..............
    10. otherString value = e-30 otherString
    11. otherString value = 4e-30 otherString
    12. otherString value = 1e-20 otherString
    13. .........................
    To copy to clipboard, switch view to plain text mode 
    I have to extract the value of field "value" in a file a compare it as it was a numerical. But the shell says that $v it's not numeric....

    Is it possible?

    thanks,
    Last edited by mickey; 20th March 2009 at 17:23.
    Regards

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

    Default Re: [perl] converting a string

    well, "e-30" is not really a (plain) number, is it?
    (are you sure the Perl string to number conversion is not limited to ints?)

    maybe the following helps
    http://search.cpan.org/~softdia/Data...ata/Str2Num.pm
    or maybe
    http://docstore.mik.ua/orelly/perl/cookbook/ch02_02.htm

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: [perl] converting a string

    I've made a mistake. I have before to extract the value
    otherString value = 4 otherString e-30

    it's a the end!
    I have the string into the variable $_
    Let that how can I extract the last part of the line?

    thanks,
    Regards

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

    Default Re: [perl] converting a string

    sorry, I don't follow you. could you perhaps rephrase that?

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    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

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

    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, 22: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.