Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

double quotes in a variable

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
pmery010
Asked:
pmery010
  • 8
  • 5
  • 3
  • +2
1 Solution
 
garfldCommented:
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
 
mrachowCommented:
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
 
RobWMartinCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ozoCommented:
% doesn't expand in qq
0
 
pmery010Author Commented:
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
 
pmery010Author Commented:
Don't mind the if statement...I cut it off because it was too long to put in the question.
0
 
RobWMartinCommented:
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
 
pmery010Author Commented:
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
 
RobWMartinCommented:
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
 
pmery010Author Commented:
Yes, you are totally right and I understand why I have the whitespace.
But there must be a way to get around that.
0
 
RobWMartinCommented:
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
 
pmery010Author Commented:
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
 
pmery010Author Commented:
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
 
RobWMartinCommented:
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
 
pmery010Author Commented:
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
 
garfldCommented:
If anyone cares, I put my comment in first in response to the original question, before mrachow jumped in with an answer.

0
 
garfldCommented:
I so seldom am able to answer anything, in this case i do deserve it!
Thank you!
0
 
ozoCommented:
#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
 
pmery010Author Commented:
Are you asking me?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now