Solved

double quotes in a variable

Posted on 2000-03-03
19
201 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now