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!

How to handle '\$' in a string? 1

Status
Not open for further replies.

lcs01

Programmer
Joined
Aug 2, 2006
Messages
182
Location
US
I am having some difficulties in handling a string with '$' embeded.

I wrote a piece of perl code to experiment:

Code:
my $text1 = "US\$ 4.5";
my $text2 = "US$ 4.5";
my $len1 = length($text1);
my $len2 = length($text2);
print "\$len1 = $len1, \$text1 = $text1\n";
print "\$len2 = $len2, \$text2 = $text2\n";

And the output is:

Code:
% ./mytest.pl
$len1 = 7, $text1 = US$ 4.5
$len2 = 4, $text2 = US.5

Apparently, I would expect $len2 is the same as $len1. But I don't know how to hadnle a string like '$text2'.

The files I got from our clients contain lots of similar stings like '$text2' shown above. Could someone offer me a hand? Many thanks!
 
'$' in a text file or user input will not be interpolated. Your examples are using double quoted strings, which will do variable interpolation on any meta characters like '$' as well as others. Are you running into problems when processing the real data your script uses or just the examples you posted?



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
lcs01,

Kevin is right in that this will not be an issue when processing files.

However, I find your side problem very curious. Kevin (or any other expert), do you know why "$ 4" is being interpolated? What is it being interpolated as? Based off of my experience, that should not be happening as a variable name cannot contain a space.

Any ideas?

- Miller
 
Code:
$ foo = 'test';
print $foo;

$f oo = 'test';
print $foo;


that should explain it [wink]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,

The first example works. When it really REALLY shouldn't.

The second example gives me a "Bareword found where operator expected at" error, which it should.

Why the heck does the first example work? If this is just a special case, then I can be ok with that. But there is no reason why that should work in string interpolation. Is there documentation talking about this crappy feature?

I feel like a gagillion potential bugs have just been introduced into all the code that I have ever written :|

TMTOWTDI Wrong,
- M
 
it's just part of perls ignoring a space(s) between a variable character and any valid string that follows.

Code:
my @    array = (1..9);
my %    hash = (foo => bar);

print @  array;
print %  hash;

foo (9);
sub foo {
   print shift;
}

I agree, it's one thing about perl I don't think should work like it does, but as long as perl can figure out what to do it will try and do it. It's covered somewhere in the perl docs but I don't remember where off the top of my head.

Of course the use of digits for scalar names is a special case and really should be avoided if possible:

Code:
$4 = 'test';
print $4;


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you both for your inputs!

Here is a bit more info:

I got a ascii file from our client. My program needs to parse it and report how long each line is. Normally, I can do something like this:

Code:
my $source = "./myfile";
open(FH, $source) || die;
while(my $line = <FH>) {
  my $len = length($line);
  # some implementation here
}
close(FH);

However, if './myfile' contains '$', then above implementation would not work properly. I have tried different ways for a string match in hoping to catch it. No luck.

Any thoughts?

Many thanks!
 
Sorry, the info above is NOT accurate. Let me study a little more and update this thread later.

Really sorry for the incorrect info.
 
lcs01, You're bannd for life from this forum.....







just kidding mate, don't worry about it, post back later if you need to.






------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Wow, I finally figured out what went wrong.

Here is the STORY:

I got a file (let's call it file A) from our client and did what the client asked for. Then the client told me my report was wrong. I took a quick look at the source file and guessed that the error could be caused by '$' embedded in the file. I manually extracted those lines and assigned them to a variable with double quots (!) and indeed found the problem that I reported here earlier. I was a bit scared!

After I saw Kevin and Miller's posts, I then realized that my code was correct. So, I ask the client how he could tell my report was wrong. He told me he had a simple tool to generate a simplifed report which does not match mine. So I looked at the source code of his simple tool and the source file (file B).

It turned out that his tool was fine but the File B is slightly different from the File A!! And the lines which are different always happen to have '$' in them! BTW, the source files are huge with very very long lines.

Whew! That's the end of the story.

Again, thank you, Kevin and Miller, so very much!
 
if length() or other string funtions like that are all you do, you should be OK. But if you use regexp on text with meta characters in it you may need to escape the meta stuff with \Q inside the regexp.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,

Could you please give me a more specific example showing how to use the '\Q' switch? My tool is basically doing all kinds string manipulations and is doing ok so far. But I always have some kind of difficulties in understanding/using regexp properly.

For instance, after reading your post, I googled '\Q' and found the following page:


in which there is a table titled 'Obscure Perl special characters' that listed '\Q', '\E', etc. But I still could not understand how to use them.

Could you please kindly show me couple examples to illustrate how to use these regexp?

Thank you very much.
 
\Q...\E is a shortcut for the quotemeta function.


There is lots of documentation, unfortunately it seems that tidbits are scattered throughout the docs for regular expressions. Just search for \Q


If you need any clarification of these docs, just ask. It's a really important feature that is best known how to use well.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top