stuck making a small change to a script
Posted by man_i_kin (man_i_kin), 12 April 2006im not a perl man, at uni it was c and c++ but ive just been given a perl script to get working. ive made most of the changes and understand what it does but i cant quite get it to work. heres the script.
basically this script reads in two text files, compares them with a name entered and spits out a nice formatted line into a batch file.
the problem is that around line 37 it stops. it does work when the text file goes only one "/" deep but if it goes deeper its stuck.
an example would be:
it can process 2YH, but it fails when it gets to 2YH\2YHLOGSAL.
please help, im totally stuck trying to extend its scope!
Posted by admin (Graham Ellis), 12 April 2006You talk about it being only "1 / deep" but then use \ in your example. Which is it you're using?
The use of a / in $foldername is fine - it's just a character - but the use of a \ will cause you a problem as you're calling up a special sequence in your regular expression.
If you must have \ characters in $foldername, add the line
$foldername = quotemeta($foldername);
directly after the line in which the variable is set up.
Posted by man_i_kin (man_i_kin), 18 April 2006Thanks for the reply, and i did make a mistake, its a "\" problem, not "/".
From what youve said would this look more suitable?
So, from my understanding, the quotemeta will strip out everything but the alpanumeric characters. Is the issue with this script that is cant handle the "\"? And therefore by removing it the script should run?
Thanks for your help again, im slowly picking up the syntax!
Posted by admin (Graham Ellis), 18 April 2006No, your change to add the quotemeta must be BEFORE the if statement in which you check it and not after. There's no point in shutting the stable door after the horse has bolted!
Add it after the
($foldername, $entryid) = split('\t');
Posted by man_i_kin (man_i_kin), 19 April 2006right, you have to excuse me, id say im just having a bad day but being overwhelmed by unknown programming languages always hurts!
and thank you for your help. works perfectly. Ive done a bit of reading up and from what i can see the problem was that it took the whole line as one and didnt recognise \ as a break in the line. is that correct?(ish)
Posted by admin (Graham Ellis), 20 April 2006Correctish ... any \x sequence where x is a letter or a digit in a regular expression (i.e. appears between slashes after an =~ operator) is taken as being a "special" - for example
\n - match a new line
\t - match a tab
\s - match a white space
\S - match a non-space
\x4A - match ASCII character 4A (hex)
\2 - match the second pattern from a previous capture.
If you REALLY want to match a \, you preceed it with another \ - in front of a character which is neither a letter nor a digit, \ means "I really want a ..." - thus \\ - I really want a \.
quotemeta adds extra \ characters to protect \ characters already in the string - so 8XM\8XMMLA becomes 8XM\\8XMMLA in your example, and so on.
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho