Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Replacing a special character

by torres09 (Acolyte)
on Jun 13, 2013 at 04:03 UTC ( #1038663=perlquestion: print w/replies, xml ) Need Help??
torres09 has asked for the wisdom of the Perl Monks concerning the following question:


I have an array with some special character like '*' and i want to print " " (space ) every where I find this '*' . So this is my code

if($dlog2[$co] eq '*') { print $fh " ,"; }

but it still prints the '*' not space . If anyone can trace out the glitch in the code . Oh and comma is desired , so its a part of code

Replies are listed 'Best First'.
Re: Replacing a special character
by davido (Archbishop) on Jun 13, 2013 at 05:13 UTC

    So are you saying that if you were to print the contents of $dlog2[$co] it would be exactly "*", nothing more, and nothing less? There's not much to trace out here: If your print $fh "  ,"; is failing to print anything to $fh, then the possibilities are:

    • $dlog2[$co] doesn't contain exactly the single character "*" and nothing else.
    • The $fh filehandle wasn't successfully opened for output.
    • You're not actually running the code you posted (either by not running it, by not posting a copy/paste of real code, or by having the entire segment of code you posted here being skipped due to errant control flow.).

    You're the only one who can trace out which of those possibilities are the cause.


Re: Replacing a special character
by vinoth.ree (Monsignor) on Jun 13, 2013 at 04:19 UTC

    Hi torres09

    Your code is correct only, if the array contains * it will print the " " (space) character, please put more code where you are printing '*' ? that could be clear to answer your question.

    All is well
Re: Replacing a special character
by pvaldes (Chaplain) on Jun 14, 2013 at 00:07 UTC
    mmmh... why not simply something like this?: s/*/* /g

      Because: Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE / at ...

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        okey, okey, it was about the idea not about the execution. The idea is that you could wish (or not) to make the replacement before populating your array. It depends on your specific case or goals

        Replacing a special character by something, full version

        while (<DATA>){ s/\*/* /g; print }; __DATA__ ******************* ********************

        Returning to the question

        I have an array with some special character like '*' and i want to print " " (space ) every where I find this '*'
        if($dlog2[$co] eq '*') { print $fh " ,"; }

        By "print everywhere" I'm understanding (maybe incorrectly) "everywhere in my array". You are not printing to your array. You are printing to a filehandle that contains and points to... well... who knows. Scalar. Not an array.

        If you want to modify your array, the idea could be: if some element of my array eq to '*' reasign again this element to ' '

        if($dlog2[$co] eq '*'){ $dlog2[$co] = ' '}

        Or maybe, to clean your problematic characters before, with a global replacement, and forget about the '*' in your file.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038663]
Approved by vinoth.ree
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2018-04-26 21:41 GMT
Find Nodes?
    Voting Booth?