Variables in Strings and Substituting a Path Name 
Author Message
 Variables in Strings and Substituting a Path Name



Quote:
> Can anyone help me with the following program?
> I've indicated what the program does, and what I
> want it to do.

Would that all posters were as thoughtful to do that as you are!

Quote:
> $a = "f:\\web page stuff";
> $b = "f:\\web page stuff\\a\\b.html";

These things are so much easier to handle using single quotes:

  $a = 'f:\web page stuff';
  $b = 'f:\web page stuff\a\b.html';

One has to overcome the C meme that single-quotes represent characters
while double-quotes represent strings.  In Perl, each represent strings.

Quote:
> $c = "../";

> $b =~ s/$a\\//;
> print "$b\n";
> # should print a\b.html (plus newline)
> # instead prints f:\web page stuff\a\b.html
> # (plus newline)

What is happening here is that the interpolated string (which, remember,
has only one actual backslash where you have typed two) is being
interpolated as if \w was a word-character class.  There are three
solutions to this problem:

1.  Worst?  Use four (4) backslashes to represent each one you want to
match against.  Strangely enough, you would have to do this even using
single-quotes.  (Why is left as an exercise.)

2.  Better?  Use quotemeta() on the string before interpolating it.

3.  Best?  Kill metacharacter interpretation in the regex:

  $b =~ s/\Q$a\E\\//;

Quote:
> $b =~ s/f:\\web page stuff\\//;
> print "$b\n";
> # should print (and does so) a\b.html
> #(plus newline)

Here, each doubled backslash becomes one literal backslash, so all is
well.

Quote:
> print "$cabc.html\n";
> # should print ../abc.html (plus newline)
> # instead prints .html (plus newline) since
> # $cabc is undefined

How is Perl to determine that you wanted to interpolate $c instead of
$cabc?  Here's how:

  print "${c}abc.html\n";

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.*-*-*.com/



Wed, 18 Jun 1902 08:00:00 GMT  
 Variables in Strings and Substituting a Path Name
Can anyone help me with the following program?
I've indicated what the program does, and what I
want it to do.

$a = "f:\\web page stuff";
$b = "f:\\web page stuff\\a\\b.html";
$c = "../";

$b =~ s/$a\\//;
print "$b\n";
# should print a\b.html (plus newline)
# instead prints f:\web page stuff\a\b.html
# (plus newline)

$b =~ s/f:\\web page stuff\\//;
print "$b\n";
# should print (and does so) a\b.html
#(plus newline)

print "$cabc.html\n";
# should print ../abc.html (plus newline)
# instead prints .html (plus newline) since
# $cabc is undefined

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Substituting $variable strings in a file

2. Substituting variables in a string?

3. Creating a variable with a file path/name in it

4. split and substitute, substitute, substitute

5. want better way to extract file name from full path file name

6. Accessing a Variable whose Name is in a String

7. Using string variable as name of subroutine to call

8. Using string content as an a variable name

9. win/dos file names as string variables?

10. reading a variable name from a string

11. Substitute File Path For URL

12. Substituting lower for upper cases in file names

 

 
Powered by phpBB® Forum Software