Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

string interpolation

by LloydRice (Beadle)
on Feb 10, 2017 at 11:36 UTC ( [id://1181645]=perlquestion: print w/replies, xml ) Need Help??

LloydRice has asked for the wisdom of the Perl Monks concerning the following question:

Can anybody help me understand what's going on here? Apparently, string interpolation is happening at some point where I did not expect it. I have a file with about 30 very long lines. The lines range from 50,000 to 70,000 chars each. I wrote a small bit of Perl expecting to cut out some short strings near the beginning of each line, but to keep the rest of each line, writing the result back to a new file.
use strict; my $me = "read52"; # Perl script to check (and revise?) the long lines in file save52.dat my $file52 = "save52.dat"; my $newfile52 = "save52x.dat"; open( S52, "<$file52" ) or die "$me: Failure opening old long-lines file '$file52'.\n"; open( NS52, ">$newfile52" ) or die "$me: Failure opening new long-lines file '$newfile52'.\n"; while ( <S52> ) { chomp; my $new = substr( $_, 5, 9 ) . substr( $_, 53 ); print NS52 "$new\n"; } close S52; close NS52;
What happened surprised me. Apparently, interpolation occurred somewhere in the process of reading or writing the long lines. Here are two cases which I studied in detail. The substring
((100% - 278px)/2)
in the original became
((100789f78<- 270 spaces ->x)/2)
in the copy. where
<- 270 spaces ->
is a way of writing
" " x 270
The substring
((100% - 206px)/2)
in the original became
((100789f78<- 198 spaces ->x)/2)
in the copy. where
<- 198 spaces ->
is a way of writing
" " x 198
What happened?? Clearly, the percent character is suspicious. But when did it get its special meaning?

Replies are listed 'Best First'.
Re: string interpolation
by Eily (Monsignor) on Feb 10, 2017 at 12:29 UTC

    That's a printf like interpolation (and % always had its special meaning in printf), where the extra spaces are padding because your pattern is "% - 206p". I'm not sure why there's a difference of 8 between that number and the number of spaces, since the interpolated value (eg: 789f78) is only 6 chars long though.

    Anyway, there's no printf in your code, so I'd say either you're reading from the wrong file, or it does not contains what you think, or the code you provided is not all that you do.

      You called it, Eily. Actually, several replies said I might have different versions of the code. And that was correct. I had switched between print and printf. When I used print, it worked OK. It was the printf that was doing the interpolation. That's good to know.
Re: string interpolation
by hippo (Bishop) on Feb 10, 2017 at 12:25 UTC
    Clearly, the percent character is suspicious. But when did it get its special meaning?

    From printf and friends. I suspect that somewhere in your original code you are accidentally doing some printf-like formatting. Can you provide an SSCCE?

      hippo, you and several others pointed out the difference between print and printf regarding interpolation. Most of my Perl info is from the llama and camel books. I just rechecked and neither one is very clear about this issue between the two. I have always used them more or less interchangeably. This is good to know. Lloyd
Re: string interpolation
by johngg (Canon) on Feb 10, 2017 at 12:03 UTC

    Try

    print NS52 $new, "\n";

    so that no interpolation takes place.

    Update: Corrected /n to \n, thanks MidLifeXis.

    Cheers,

    JohnGG

      John, This one sounded like it would work. Sadly, it did not help. I now understand that what happened is that printf forces interpolation with or without the double-quotes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1181645]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found