4

Could someone help me in correcting me for the following code. I want to extract the two numbers from a input string.

  input string [7:0] xxxx

I want '7' and '0' to be loaded into two variables (min and max). I am trying to achieve this by

my ($max, $min);
($max, $min) = $_ =~ /[(\d+):(\d+)]/;
print "min: $min max $max\n";

I am getting a result as

Use of uninitialized value in concatenation (.) or string at constraints.pl line 16, <PH> line 165.
min:  max: 1

regards

0

2 Answers 2

7

[ and ] are regex meta characters, so you have to escape them

($max, $min) = $_ =~ /\[(\d+):(\d+)\]/;

The brackets are used to denote a character class: [ ... ] which matches the characters inside it, e.g. [abc] matches a.

Sign up to request clarification or add additional context in comments.

3 Comments

And additionally, you can combine lines 1 and 2, like this: my ($max, $min) = $_ =~ /[(\d+):(\d+)]/; I like to declare and assign in one step whenever that's possible.
@Andrejovich If you keep the backslashes, yes you can. Normally, I would say you would see the shortest form of them all: my ($max, $min) = /\[(\d+):(\d+)\]/, where the $_ =~ part is implied. This often looks confusing to beginners.
d'oh, you're absolutely right. I should've copy/pasted your code, not the OP's.
0

TLP is correct. [] is meta character and any such character is required escaping like . () [] * etc to use it for literal match. This would solve your problem.

($max, $min) = $_ =~ /\[(\d+):(\d+)\]/;

You may get warning if $max or $min or both would be blank i.e. [ 7: ] or [ : ] or [ : 2] or [ ] .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.