0

I have this code and having matched the input, I want it split into two separate variables. The input is in the form of a range (e.g: 1-10) and is written in perl.

Here is the code: [ if($pr=~m/^([0-9]+)\s*[-]\s*([0-9]+)$/){ print "Match"; } else { print "Not a match"; } ]

Thanks.

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by ibdatx
0

To rephrase properly, I have this code and having matched the input, I want it split into two separate variables. The input is in the form of a range (e.g: 1-10) and is written in perl.

Here is the code:

sub regX()
{
print "Enter port range (e.g 1-2): "; chomp($pr=<STDIN>);
if($pr=~m/^([0-9]+)\s*[-]\s*([0-9]+)$/){ print "Match"; }
else { print "Not a match"; }
}

Thanks.

0

Hi all,

This is a perl script. I have this input that I intend to split into multiple variables, however the variables $1 and $2 work for a limited number of inputs. Please run code and see first then provide an advice. Thanks for your suggestions.

sub regX()
{
print "Enter numbers with ',' as delinmeter (e.g.: 1,2,3,...): ";
chomp($pr=<STDIN>);
if($pr=~m/^(([0-9]+)\s*[,]\s*([0-9]+)*)+$/)
{
#print multiple variables up to n inputs
}
else { print "Unmatched entry"; }
}

0

Why don't you just split them on the "," into an array?

$count=(@array)=split(/\,/,$pr);

Your regX is not going to work the way that you wish for it to. When I ran the code I input "1,2,3" and got "1 2,3" in $1.

0

Why don't you just split them on the "," into an array?

$count=(@array)=split(/\,/,$pr);

Your regX is not going to work the way that you wish for it to. When I ran the code I input "1,2,3" and got "1 2,3" in $1.

True statement mitchems...I'll try that. Thanks for the quick response.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.