Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How can I match between two integer numbers

by Anonymous Monk
on Jan 02, 2003 at 12:00 UTC ( #223756=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

50545 4.393864E+00 .0 50546 9.589456E+00 .0 50547 2.690474E+00 .0 50548 -1.668896E+01 .0 50549 -3.391918E+01 .0 50550 -3.634581E+01 .0 50551 -3.176630E+01 .0 50552 -1.747830E+01 .0 50553 -5.256684E-01 .0 50554 1.135265E+01 .0
I am trying to match between two integers. For example In the first line I tried to match anything between 50545 and 50546 and after 50546. In the first line I want to match "  4.393864E+00  .0" and stote it in "$1" and match "  9.589456E+00  .0" and store it in "$2". Do the same for the other lines. My code was:
if($in =~ m/^\s+\d+\s+(.*)\d+\s+(.*)$/){ print FH ("$1\n"); print FH ("$2\n"); }
This code did not work. Can you help me?

Replies are listed 'Best First'.
Re: How can I match between two integer numbers
by davorg (Chancellor) on Jan 02, 2003 at 12:15 UTC

    This seems to give the answers you want

    /^\s+\d+\s+(.*?)\s+\d+\s+(.*?)$/;

    It's worth looking at the $`, $& and $' variables to help you debug regular expressions.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      If he want match also leading space (" 4.393864E+00 .0" instead of "4.393864E+00 .0") I suggest

      /^\s+\d+\s(.*?)\s+\d+\s(.*?)$/
        It's worth looking at the $`, $& and $' variables to help you debug regular expressions.

      It's relavant to note that the speed of the regexp code in Perl decreases when these variables are used.

      --t. alex
      Life is short: get busy!

        Absolutely. Use them for debugging, but don't use them in production code - unless you really need to :)

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: How can I match between two integer numbers
by talexb (Chancellor) on Jan 02, 2003 at 16:23 UTC

    Unless I've missed something, can't you just use split to get the job done?

    while (<DATA>) { my @Data = split ( /\s+/ ); print "Compare $Data[2] and $Data[5].\n"; } __DATA__ 50545 4.393864E+00 .0 50546 9.589456E+00 .0 50547 2.690474E+00 .0 50548 -1.668896E+01 .0 50549 -3.391918E+01 .0 50550 -3.634581E+01 .0 50551 -3.176630E+01 .0 50552 -1.747830E+01 .0 50553 -5.256684E-01 .0 50554 1.135265E+01 .0

    This code seems to work fine.

    --t. alex
    Life is short: get busy!

    Update: D'Oh! Sorry -- I thought the objective was to compare the numbers in exponent notation, not the integers before them. Thanks gjb.

      Or, variation on the theme above, but keeping to what was asked (i.e. matching between the integer, not matching the individual real numbers):

      while (<DATA>) { chomp($_); my ($dummy, @Data) = split ( /\s+\d+\s+/ ); print join(", ", @Data), "\n"; } __DATA__ 50545 4.393864E+00 .0 50546 9.589456E+00 .0 50547 2.690474E+00 .0 50548 -1.668896E+01 .0 50549 -3.391918E+01 .0 50550 -3.634581E+01 .0 50551 -3.176630E+01 .0 50552 -1.747830E+01 .0 50553 -5.256684E-01 .0 50554 1.135265E+01 .0

      Hope this helps, -gjb-

Re: How can I match between two integer numbers
by hardburn (Abbot) on Jan 02, 2003 at 14:49 UTC

    You're probably having problems with the greedy-ness of .* and +. See also Death to Dot Star!.

Re: How can I match between two integer numbers
by CountZero (Bishop) on Jan 02, 2003 at 16:47 UTC

    Activestate has Komodo a nice IDE for Perl (and other languages). It has a handy "real time" Regular Expression tool, which I use quite a lot to fiddle with Regexes.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      The original version is available from mjd's site as well.

      Personally I find that perl -de 0, setting $_, and incrementally piddling with x /foo(.*?)bar/ until I get something that matches works pretty well.

Re: How can I match between two integer numbers
by JamesNC (Chaplain) on Jan 02, 2003 at 19:17 UTC
    Maybe this helps?
    use strict; while(<DATA>){ $_ =~s /\s+/:/ig; my ($a, $b, $c, $d, $e, $f) = split /:/; my $A = int scalar $a; #marker my $B = int scalar $b; #data my $C = int scalar $c; #data my $D = int scalar $d; #marker my $E = int scalar $e; #$data my $F = int scalar $f; #$data print "SCALAR: $a-> $b, $d-> $e\n"; print "INT PART: $A-> $B, $D-> $E\n"; # print FH "$B\n$E\n"; # your FH } __DATA__ 50545 4.393864E+00 .0 50546 9.589456E+00 .0 50547 2.690474E+00 .0 50548 -1.668896E+01 .0 50549 -3.391918E+01 .0 50550 -3.634581E+01 .0 50551 -3.176630E+01 .0 50552 -1.747830E+01 .0 50553 -5.256684E-01 .0 50554 1.135265E+01 .0
Re: How can I match between two integer numbers
by osama (Scribe) on Jan 03, 2003 at 20:55 UTC
    why not just do something like this:
    @fields=split(/\s+/); print FH "$fields[1] $fields[2]\n"; print FH "$fields[4] $fields[5]\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://223756]
Approved by IlyaM
Front-paged by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2023-10-03 00:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?