Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

double quotes in a variable

Posted on 2000-03-03
19
222 Views
Last Modified: 2010-03-05
I would like to have an output string that has double quotes in it but I am not sure how to do it.
i.e.
$outputstring=" "%14s",linearray[0];"
print $outputstring;

Does anybody know how I can accomplish something like this?
0
Comment
Question by:pmery010
  • 8
  • 5
  • 3
  • +2
19 Comments
 
LVL 2

Accepted Solution

by:
garfld earned 100 total points
ID: 2580610
Just put an escape in front of the qq, like so:
$mystring = "<h4>Your pizza will be ready in \"15\" minutes!</h4><br>\n";
print $mystring;

Betsy
0
 
LVL 1

Expert Comment

by:mrachow
ID: 2580634
You simply have to escape every " inside a string by \ like:
$outputstring=" \"%14s\",linearray[0];";
BTW there were no ; at the end of your string assignment. Maybe you got an error from that in addition?
Remember that you have to escape things like $ and @ in strings too. Otherwise Perl try to expand something.
Greetings
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2580770
You don't have to escape each individual double quote if you use a different surrounding quote:

$outputstring=qq{"%14s", linearray[0];};

or

$outputstring=q{"%14s", linearray[0];};

to keep from expanding %,@,$.


0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 84

Expert Comment

by:ozo
ID: 2580862
% doesn't expand in qq
0
 

Author Comment

by:pmery010
ID: 2581563
I don't know what I am doing wrong but I am not getting the output I am expecting.

$outputstring="%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s\n\",$LineArray[15],$LineArray[29],$LineArray[1],$LineArray[0],$LineArray[9],$LineArray[10],$LineArray[2],$LineArray[23],$LineArray[4],$LineArray[18],$LineArray[19];
foreach $TossLine(@TossResult)
            {
      @LineArray=split("\t",$TossLine);
            if (((substr($LineArray[0],0,2) eq "C2") || (substr($LineArray[0],0,2) eq "1A") || (substr($LineArray[0],0,2) eq "1B") || ({
                  printf OutFile $outputstring;

                  }#end if
            }#end for
      close(OutFile);

For some reason the LineArray is not resolving properly.  The output I am getting looks like:

",,,,,,,,,,,;"     ,     ,     ,     ,     ,     ,     ,     ,     ,     ,

",,,,,,,,,,,;"     ,     ,     ,     ,     ,     ,     ,     ,     ,     ,

",,,,,,,,,,,;"     ,     ,     ,     ,     ,     ,     ,     ,     ,     ,

",,,,,,,,,,,;"     ,     ,     ,     ,     ,     ,     ,     ,     ,     ,

",,,,,,,,,,,;"     ,     ,     ,     ,     ,     ,     ,     ,     ,     ,

",,,,,,,,,,,;"     ,     ,     ,     ,     ,     ,     ,     ,     ,     ,

",,,,,,,,,,,;"     ,     ,     ,     ,     ,     ,     ,     ,     ,     ,

I know that there is nothing wrong with the output string because when I put it in the output string literally I get the proper output

What am I doing wrong?
0
 

Author Comment

by:pmery010
ID: 2581574
Don't mind the if statement...I cut it off because it was too long to put in the question.
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2581693
Not so sure how this is getting past the compiler:

$outputstring="%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s\n\",$LineArray[15],$LineArray[29],$LineArray[1],$LineArray[0],$LineArray[9],$LineArray[10],$LineArray[2],$LineArray[23],$LineArray[4],$LineArray[18],$LineArray[19];

Assuming the real code has this:

$outputstring="\"%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s\n\",$LineArray[15],$LineArray[29],$LineArray[1],$LineArray[0],$LineArray[9],$LineArray[10],$LineArray[2],$LineArray[23],$LineArray[4],$LineArray[18],$LineArray[19];";

When this is executed you have the $LineArray[#] terms resolve to empty strings.  By the time you get to the loop that walks thru @TossResult, your $outputstring looks like this:


",,,,,,,,,,,;"     ,     ,     ,     ,     ,     ,     ,     ,     ,     ,


The first part is the result of concat'ing all those empty $LineArray[#]'s and commas.  The large whitespaces separated by commas result from printf have %s specs with nothing to fill them.

Try this instead:

foreach $TossLine(@TossResult)
{
@LineArray=split("\t",$TossLine);
if (((substr($LineArray[0],0,2) eq "C2") || (substr($LineArray[0],0,2) eq "1A") || (substr($LineArray[0],0,2) eq "1B") || ({
printf OutFile "%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s\n",$LineArray[15],$LineArray[29],$LineArray[1],$LineArray[0],$LineArray[9],$LineArray[10],$LineArray[2],$LineArray[23],$LineArray[4],$LineArray[18],$LineArray[19];

}#end if
}#end for
close(OutFile);




0
 

Author Comment

by:pmery010
ID: 2581711
That is what I used to have.
What I want is to be able to specify the output string by passing a variable to the function.
I would like to be able to use the same function and print out different elements at different times.
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2581730
I got carried away with the cut and paste and then skipped a step.  $outputstring looks like this:

"%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s
",,,,,,,,,,,;    

when you enter the loop.  Note, there is a newline in there.  For the output, then, the first part is the newline and ",,,,,,,,,,,;  The whitespace areas separated by commas is as explained in my previous comment.

Nonetheless, the problem comes from building $outputstring with array elements that have no values yet.



0
 

Author Comment

by:pmery010
ID: 2581742
Yes, you are totally right and I understand why I have the whitespace.
But there must be a way to get around that.
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2581753
To do what you want to do, you will need to pass the format string and an array of indices into @LineArray.  Then in the function slice @LineArray by the array of indices.

call this way:
genfunc("%-5s....",15,29,1,0,...);


here's the function:
sub genfunc(){
    $fmt=shift;
    @indices=@_;

    ....

    printf Outfile $fmt, @LineArray[@indices];

    .....

You can fill in the blanks and let us know how it works.

0
 

Author Comment

by:pmery010
ID: 2581772
Good Call....passing the indices as an array was my next idea but I had a much more archaic method in mind.  Your method is much better.
Thanks.
I am going to reject the original answer and can you put your comment in as an answer.  
0
 

Author Comment

by:pmery010
ID: 2581791
Good Call....passing the indices as an array was my next idea but I had a much more archaic method in mind.  Your method is much better.
Thanks.
I am going to reject the original answer and can you put your comment in as an answer.  
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2581794
I'm glad you're happy with the solution.  However, technically speaking, mrachow did answer your original question sufficiently.  He/She should get the points.

G'day,
Rob
0
 

Author Comment

by:pmery010
ID: 2581820
I applaud your sense of fair play.
Sorry I guess I got a little a head of things.  
I have already rejected the answer but if mrachow wants to post again I will accept his answer.
If not I will accept RobWMartin's.
Thanks again.
0
 
LVL 2

Expert Comment

by:garfld
ID: 2581918
If anyone cares, I put my comment in first in response to the original question, before mrachow jumped in with an answer.

0
 
LVL 2

Expert Comment

by:garfld
ID: 2581931
I so seldom am able to answer anything, in this case i do deserve it!
Thank you!
0
 
LVL 84

Expert Comment

by:ozo
ID: 2581948
#did you mean to say
$outputstring=sprintf "%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s,%-5s\n",@LineArray[15,29,1,0,9,10,2,23,4,18,19];
0
 

Author Comment

by:pmery010
ID: 2581953
Are you asking me?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
pattern matching in perl 2 105
How to delete rows in a file based on the file name. 5 61
Perl string replace for refred url 9 63
create a gui in perl 3 98
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question