1834814 Members
2510 Online
110070 Solutions
New Discussion

Re: if loop

 
SOLVED
Go to solution
Allanm
Super Advisor

if loop

Can someone please explain the if loop here used in a perl script :

if (m!\[(\d+)\]!)

Thanks,
Allan
6 REPLIES 6
Venkatesh BL
Honored Contributor

Re: if loop

Am not a perl guy, but, looks like a pattern check...did u try google?
Peter Nikitka
Honored Contributor

Re: if loop

Hi,

the if-condition ('if' is not loop) is true when matching a number in square brackets, like [12].

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Allanm
Super Advisor

Re: if loop

Thanks for replying Peter, please find attached the exact script. Could you please explain what its doing. I am not understanding the if conditions here.

Thanks.
Allan
Peter Nikitka
Honored Contributor

Re: if loop

Hi,

I suspect this scripts tries to hack a website.
Is that really your idea?

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor
Solution

Re: if loop

Hi:

This is a test for a match using a regular expression.

The 'm' means match. Perl allows many different delimiters to bracket the match and in this case the '!' character was chosen.

The opening ('[') and closing (']') bracket characters are escaped with a backslash so that they represent themselves in the match and not a character class.

The '\d+' stands for a digit that occurs one or more times but at least once.

The parentheses () surrounding the '\d+' tell the regular expression engine to capture any match it finds therein. The outer parentheses are syntax for the 'if' condition.

In all, the condition is true (a match) if the variable ($_) --- often a line read --- looks like:

[123]

If you do:

# perl -nle 'print $1 if (m!\[(\d+)\]!)'

...then you will see the digits that are surrounded by the square brackets echoed for every input that matches.

Regards!

...JRF...
Allanm
Super Advisor

Re: if loop

Thanks JRF , appreciate it!