Solved

Test condition for pattern match

Posted on 2007-11-21
6
504 Views
Last Modified: 2008-02-07
Please consider following code snippet.  The value of file_path is ..\..\..\src\app\test\filename.c.  What is
[ \  /   \  \ ].  Note:  There is no whitespace inside brackets.  I put whitespace so contents inside parentheses is visible to the experts.  Is the if condition true?
paths_hash is a hash variable.  build_path is M:\view_name\lib\subdir\target\app\test
What does the statement inside if condition do?

if ( $file_path !~ m/[\  /  \  \  ]/ )
{
    $paths_hash{$build_path} = 1;
}
0
Comment
Question by:naseeam
6 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 200 total points
ID: 20329352
The if statement is looking to see if $file_path contains either a forward slash "/" or a backslash "\".
So when $file_path = "..\..\..\src\app\test\filename.c", the if condition is true.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20329892
Adam, I think you meant the opposite.

The if statement is looking to see if $file_path does NOT contain either a forward slash "/" or a backslash "\".

!~ vs =~ operator
0
 
LVL 1

Author Comment

by:naseeam
ID: 20329918
So forward and backward slash can be in any position.  It doesn't matter that there are two dots before the backward slash.  

Thanks.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 160 total points
ID: 20330041
Yes, the forward or backslash could be at any point in the path.  If either are found, then the result of the conditional is false.

Both of these conditionals will have the same results

if ( $file_path !~ m/[\  /  \  \  ]/ )

unless ( $file_path =~ m/[\  /  \  \  ]/ )
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 80 total points
ID: 20330860
The [] is a character class, it mean match ANY of the chars inside it. In this case the chars are \ and / (both escaped) so it'll match either. The != negates this logic so, as stated above, it means don't match.

So the code means...

if no match is found for \ or / in $file_path then $paths_hash{$build_path} equals 1.

More info on character classes: -

http://www.regular-expressions.info/charclass.html

-Rx.

0
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 60 total points
ID: 20341696
In summation, $paths_hash{$build_path} is set to equal 1 if the variable $file_path does not contain any forward or backward slashes.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

830 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