Solved

Test condition for pattern match

Posted on 2007-11-21
6
496 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

11 Experts available now in Live!

Get 1:1 Help Now