Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Changing %s to (Info: %s)

by theroninwins (Friar)
on Apr 12, 2006 at 08:44 UTC ( [id://542771]=perlquestion: print w/replies, xml ) Need Help??

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

Ok here is my problem I have a file that contains %s and I need those to become (Info: %s).

Here is what I have :
#!/usr/bin/perl use warnings; use strict; open READER,"<HWM.err"; open WRITER,">>final.err"; while( my $line = <READER> ) { chomp $line; #$linefinal= sprintf $line, "(Info: %s)"; (my $linefinal = $line) =~s/%s/(Info: %s)/g; #(my $linefinal = $line) =~s/\%s/(Info: \%s)/g; printf WRITER "$linefinal\n"; } close READER; close WRITER; __DATA__ 6101;CON_RST_OCC;3;3;0;0;Controller reset occured %s; Zurücksetzung de +s Controllers ist erfolgt %s 6102;DEG_UNIT;3;3;0;0;Degraded unit %s;Unit wurde runtergestuft %s
I tried to use \%s in the s statement but that didn't help either.
Can anyone help me please?
Thanks

OK I updated the code

Replies are listed 'Best First'.
Re: Changing %s to (Info: %s)
by Corion (Patriarch) on Apr 12, 2006 at 08:49 UTC

    The =~ operator and s/// don't work the way you use them. You should also be seeing some warnings from Perl about

    Useless use of private variable in void context at ... line 10.

    which should have made you suspicious about your code.

    I think what you likely want is to learn about how =~ applies to variables, via perlop and perlre. I guess the following line could meet your intentions:

    (my $linefinal = $line) =~ s/.../.../;

    I see that you took some time to actually gather test input and output, but please test your posted code before posting it - your code doesn't read from the DATA handle.

      Thanks I inserted it and now it does make some changes only that it cuts out the %s just writing (Info: )...

      This is the message (with \%s OR %s in the s statement):
      Use of uninitialized value in printf at ./changer.pl line 11, <READER> + line 73.

        Could you try the code as posted? That error shows you are still reading from a different file handle than DATA. Try getting the examples posted here working and then once those work adapt them to your own needs.


        ___________
        Eric Hodges
Re: Changing %s to (Info: %s)
by GrandFather (Saint) on Apr 12, 2006 at 08:54 UTC

    Where does $linefinal get a value? Where is your use strict; (which would have shown the problem)?

    Why do you have a __DATA__ section, but open and manipulate a couple of files? It seems that you might have been trying to come up with a sample like this:

    use strict; use warnings; while (<DATA>) { s/%s/(Info: %s)/g; print; } __DATA__ 6101;CON_RST_OCC;3;3;0;0;Controller reset occured %s; Zurücksetzung de +s Controllers ist erfolgt %s 6102;DEG_UNIT;3;3;0;0;Degraded unit %s;Unit wurde runtergestuft %s <p>
    which prints:

    6101;CON_RST_OCC;3;3;0;0;Controller reset occured (Info: %s); Zur³ckse +tzung des Controllers ist erfolgt (Info: %s) 6102;DEG_UNIT;3;3;0;0;Degraded unit (Info: %s);Unit wurde runtergestuf +t (Info: %s)

    DWIM is Perl's answer to Gödel
      OK I take the guilt for that last replay of mine... the code works fine... I didn't see the print (no f) when I included it into mine.. Sorry (won't happen again) Thanks for all your help...
        'I didn't see the print (no f) when I included it into mine'

        Which is why you should have used the download link provided then tried GrandFathers code before saying that it didn't work.

        Martin
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-16 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found