Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

regular expression problem

Posted on 2003-03-18
9
Medium Priority
?
145 Views
Last Modified: 2010-03-05
$string1="1+";
$string2="2+3+4+";

while($string1=~/([^\+]+)/g)
  {$a=$1;
   print("a=$a\n");
   while($string2=~/([^\+]+)/g)
     {$b=$1;
      print("b=$b\n");
      &SomeMoreRegularExpressions($a,$b);
     }
  }

This code is supposed to print output:
a=1
b=2
b=3
b=4

But it prints
a=1
b=2
b=3
b=4
b=3
b=4
b=4
b=4

What is wrong? How can I fix it?
0
Comment
Question by:serg111
9 Comments
 
LVL 8

Expert Comment

by:jhurst
ID: 8163820
you are doing a while and not chabnging the condition tested in the while.  
0
 
LVL 3

Expert Comment

by:prady_21
ID: 8164086
I tested your code on my system and it is working perfect.
I think you should check the regular expressions after the
print statement.ie after  --print("b=$b\n");

i just did cut and paste and you can check the result


#!/usr/bin/perl

### a test program

$string1="1+";
$string2="2+3+4+5+";

while($string1=~/([^\+]+)/g)
 {
  $a=$1;
  print("a=$a\n");
  while($string2=~/([^\+]+)/g)
    {
     $b=$1;
     print("b=$b\n");
    }
 }





%./resttest.pl
a=1
b=2
b=3
b=4
b=5
0
 
LVL 85

Expert Comment

by:ozo
ID: 8164144
what does
&SomeMoreRegularExpressions($a,$b);
do?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:desktop2
ID: 8164209
Problem is somewhere in this function SomeMoreRegularExpressions since without it the while with regular expression is working fine


sub SomeMoreRegularExpressions{
local($ip,$port)=@_;
local(@output,$output,$path);
$childID=fork();
if($childID)
  {return 0; }
if($PathWww=~/([^\/]+)(\/.+)/)
 {$ip=$1;
  $path=$2;
 }
else
 {$path="/";
 }
$socket=IO::Socket::INET->new(Proto=>"tcp",PeerAddr=>$ip,PeerPort=>$port,Reuse =>1,Timeout=>40);
$request="POST $path HTTP/1.0\nContent-Type: application/x-www-form-urlencoded\nContent-Length: 7\n\nr=test\n";
print $socket $request;
@output = <$socket>;
# ===============================
foreach(@output)
  {$output.=$_;}
close($socket);

if($output eq "OK" || ($output=~/\r?\n\r?\nOK/g))
 {return 1; }
else
 { exit; }
}
0
 

Expert Comment

by:desktop2
ID: 8164219
actually $string1 and $string2 contains names of servers and port number to connect
0
 
LVL 20

Accepted Solution

by:
jmcg earned 800 total points
ID: 8168380
If the code Desktop2 found in one of your other questions is still the code you're using, the explanation of the behavior you are seeing is pretty clear:

Each value found in string 2 results in an additional process. When the output of the request handled by the child is "OK", you end up with both processes continuing to process the list in string2. The first value gets processed by the first child. The original process and the first child both process the second value. And each of them creates a child so there end up being 4 processes working on the 3rd value.

The obvious fix is to change your subroutine so that the child process always exits.
0
 

Expert Comment

by:desktop2
ID: 8168446
Thanks, it helped!
mmm... can't find any button to accept this answer
0
 
LVL 2

Author Comment

by:serg111
ID: 8168461
Sorry - I've connected from home computer with different username
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8168676
Ah, the mystery of how desktop2 knew what was in the subroutine is now solved.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question