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

Novice here, I am trying to convert a '^' to a ':' but I am unable to. My script pulls in several data files and parses them based on ':' into an array. I tried using $_ =~ s/^/:/g; and $_ =~s/\^/:/g; but neither seems to work. Oh wise monks what would you suggest?

Replies are listed 'Best First'.
Re: ^ to :
by Roy Johnson (Monsignor) on Jun 09, 2004 at 15:22 UTC
    For substituting individual characters without regard to context, tr/^/:/ is the right tool.

    The PerlMonk tr/// Advocate
      That did it, thanks oh wise one.
Re: ^ to :
by alienhuman (Pilgrim) on Jun 09, 2004 at 15:14 UTC

    Hi new_monk,

    I suggest that you post your code in its entirety, because your second regexp s/\^/:/g certainly works in the snippet that I wrote up to test it. My guess is that something else in your code is causing the problem.

    AH

    ----------
    Using perl 5.6.1 unless otherwise noted. Apache 1.3.27 unless otherwise noted. Redhat 7.1 unless otherwise noted.
Re: ^ to :
by pbeckingham (Parson) on Jun 09, 2004 at 15:12 UTC

    What you have described is correct, given the lack of context. Are you writing out the files afterwards? Have you done any before/after comparisons on $_?

    Could you post a more complete code sample?

Re: ^ to :
by ambrus (Abbot) on Jun 09, 2004 at 15:27 UTC

    Do you know how the -p and -i options work?

    Do you write the data back to the file?

Re: ^ to :
by tachyon (Chancellor) on Jun 10, 2004 at 03:20 UTC

    You could do an inplace edit s/'/"/ if $^O =~ m/MSWin32/ Backups made to file.bak

    perl -pi.bak -e 'tr/^/:/' file list here

    But from what you write rather than bother to convert ^ to : if the actual need is to split on ^ OR : chars you should note that the first arg to split is really an RE so:

    split /[\^:]/, $data

    cheers

    tachyon