Solved

Test condition for pattern match

Posted on 2007-11-21
6
499 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl count the hash for print 4 161
Perl Sort Question 4 129
Having a problem with ActiveState Perl on 64-bit Windows 7 3 68
Parse csv file and generate graphs in HTML in bash 8 156
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now