CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Putting double quotes around a string

 



perlFun
Novice

Apr 24, 2014, 6:26 PM

Post #1 of 10 (3206 views)
Putting double quotes around a string Can't Post

I'm having a simple problem. I start out with a string, say, something like this:

$foo = "string";

if you print this, it will just print string

Now, when I print this variable, or use it anywhere, I want double quotes included on it.

I can't seem to do, $foo = "$foo"; because this just seems to be a concatenation operation.


Similarly I tried, $foo = qq($foo); and this does not work as well.


I am curious how I can get the double quotes on my string


(This post was edited by perlFun on Apr 24, 2014, 6:27 PM)


2teez
Novice

Apr 24, 2014, 7:18 PM

Post #2 of 10 (3203 views)
Re: [perlFun] Putting double quotes around a string [In reply to] Can't Post

Hi,

And if you do so:

Code
my $str = q["string"]; 

print $str;

What do you have?

Please, check the link below:
http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators


BillKSmith
Veteran

Apr 24, 2014, 8:30 PM

Post #3 of 10 (3200 views)
Re: [perlFun] Putting double quotes around a string [In reply to] Can't Post

You almost have it. The quotes that I add become part of the string.


Code
use strict; 
use warnings;
my $foo = 'string';
$foo = qq("$foo");
print "This is the $foo that you want.\n";


OUTPUT:
This is the "string" that you want.

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Apr 24, 2014, 11:43 PM

Post #4 of 10 (3198 views)
Re: [perlFun] Putting double quotes around a string [In reply to] Can't Post

Or you could just do the following:


Code
$foo = '"string"';

or

Code
$bar = 'This is a "string" within quotes.';



perlFun
Novice

Apr 25, 2014, 2:00 PM

Post #5 of 10 (3188 views)
Re: [perlFun] Putting double quotes around a string [In reply to] Can't Post

Oh hey thanks a lot for the responses everyone. yes, $string = qq("$string"); seemed to word swell for this.


I'm kind of curious though why it has to go like that, the documentation I read for qq indicated that whatever you supply to the qq function, qq returns that string but with quotes on it. So I am curious why those "extra" quotes are needed?


BillKSmith
Veteran

Apr 25, 2014, 9:12 PM

Post #6 of 10 (3154 views)
Re: [perlFun] Putting double quotes around a string [In reply to] Can't Post

The four operators q(), qq(), "", and '' all do nearly the same thing. They tell perl that the contents are a string. In your case, you want quote characters to be the first and last characters of your string. The quotes that you describe as 'extra' become those characters. You still need a way to tell perl that this is a string.

q() or '' cannot be used because they do not interpolate the variable. qq() and "" differ in the way that they treat quote characters. We choose qq() because it interprets quote characters as as ordinary characters in the string (exactly what we need).

I hope that I have given you the clue that you need to understand the documentation of these four operators.
Good Luck,
Bill


perlFun
Novice

Apr 25, 2014, 10:25 PM

Post #7 of 10 (3129 views)
Re: [BillKSmith] Putting double quotes around a string [In reply to] Can't Post

Yes, this was a very helpful explanation thank you!


2teez
Novice

Apr 26, 2014, 2:19 AM

Post #8 of 10 (3077 views)
Re: [BillKSmith] Putting double quotes around a string [In reply to] Can't Post


Quote
q() or '' cannot be used because they do not interpolate the variable. qq() and "" differ in the way that they treat quote characters. We choose qq() because it interprets quote characters as as ordinary characters in the string (exactly what we need).


Be that as it may, I wondering why the OP has to do in 2 steps, what can be achieved in a single step.

Please check the examples below.

Code
use warnings; 
use strict;

my $str = q["hello"];

print "Example 1\n";
print "$str, world";

print "\n\nExample 2\n";
my $another_str = "I came home to say a big $str";

print $another_str;

print "\n\nExample 3\n";

$str = 'good-bye';
$str = qq["$str"];

print "$str, world";

print "\n\nExample 4\n";
$another_str = "I came home to say a big $str";

print $another_str;


OUTPUT

Code
Example 1 
"hello", world

Example 2
I came home to say a big "hello"

Example 3
"good-bye", world

Example 4
I came home to say a big "good-bye"

Please compare example 1,2 with 3,4.
The point is since q() and '' don't interpolate we use this to an advantage. Put in it the string you want with the quote and you get what you wanted, except I missed what the OP wanted or what he is trying to achieve.


(This post was edited by 2teez on Apr 26, 2014, 2:24 AM)


BillKSmith
Veteran

Apr 26, 2014, 7:36 AM

Post #9 of 10 (3068 views)
Re: [2teez] Putting double quotes around a string [In reply to] Can't Post

If the OP were able to use accurate terminology in his question, he probably would not have to ask it.Smile I attempted to answer his question, using as much of his work as possible. Of course there is more-than-one-way-to-do-it. No one even suggested escaping the quotes.
Good Luck,
Bill


2teez
Novice

Apr 26, 2014, 12:43 PM

Post #10 of 10 (2922 views)
Re: [BillKSmith] Putting double quotes around a string [In reply to] Can't Post


Quote
..No one even suggested escaping the quotes...

That would be a bit ugly you know.... Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives