Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed with wildcards

Status
Not open for further replies.

wdiaz102

MIS
Sep 10, 2007
1
US
I have a script that will create a directory based on user input. The directories are named numerically. For example, some could be 210999 and others could be 2813220. I want to match anything that starts with a 2 but has only 6 digits to be created in a parent folder named 200. Anything that starts with 28 and has 7 digits like the example above to be created in the parent folder called 2800. Part of my lines look like:

elsif ($Proj =~ /2\d\d\d\d\d/)

mkdir ($p200.$Proj, 0777)

elsif ($Proj =~ /28\d\d\d\d\d/)

mkdir ($p2800.$Proj, 0777)

I left some of the symbols out for clarity. There are a lot of other folders with the same type of code as above and everything is working fine, except when I input 2813220 into the text box, the folder is created in the 200 and not the 2800 parent folder. How can I write it so that it matches the first two digits, 28, and then counts 5 digits. It seems to me that the number of digits are not matching. I hope this makes sense, please let me know if there's a better way for me to do this. thanks


 
Assuming everything else is OK with your code, just reverse the order of your regexps and it should work:

Code:
elsif ($Proj =~ /28\d\d\d\d\d/)

mkdir ($p2800.$Proj, 0777)

elsif ($Proj =~ /2\d\d\d\d\d/)

mkdir ($p200.$Proj, 0777)

The problem is you are only doing substring matches. It will work like you had it if you add string anchors:

Code:
elsif ($Proj =~ /^2\d\d\d\d\d$/)

mkdir ($p200.$Proj, 0777)

elsif ($Proj =~ /^28\d\d\d\d\d$/)

mkdir ($p2800.$Proj, 0777)

but that assumes the pattern is the only thing in $Proj

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top