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

I'm using mySQL.

I perform my fetch as $job_hash = $sth->fetchrow_hashref();

Next, I want to subsitute the "\n" that are part of a particular element and replace them with "&ltbr&gt" for HTML display.

i.e.

$$job_hash{'subjects'} =~ s/\n/<br>/g;
What I end up with is seemingly garbled $$job_hash{'subjects'} which randomly removes portions of the text and also seems to include "back-spaces" as it won't even print out
print "subjects = $$job_hash{'subjects'}\n"; correctly. Instead, it appears to print out the right hand side of $$job_hash{'subjects'}, backspaced on top of "subjects = ".
???? What am I doing wrong here.

I've used the same regex before on the 'subjects' field when using $sth-&gtfetchrow_array and it has worked fine. Is there something about referenced hashes that I'm not understanding? (I've even tried assigning it to a seperate variable ($sub = $$job_hash{'subjects'};) and then performing the regex, but still no luck
--
Filmo the Klown

Replies are listed 'Best First'.
Re (tilly) 1: Strange values in hash reference
by tilly (Archbishop) on Aug 14, 2001 at 14:44 UTC
    My bet is that you are running on a *nix platform and the data in the database includes \r\n sequences. Therefore when you display a line it displays the line, does a carriage return, and then (since you don't do the newline advance) displays the next line on top of the old.

    Try stripping out \r and see if that makes a difference.

      Thanks, this totally fixed the problem. New code =

       $$job_hash{'subjects'} =~ s/\r\n/<br>/g;
      --
      Filmo the Klown

        Be careful. The line endings in your database could be inconsistent. Try:
        s/\r\n?|\n\r?/<br>/g;
        which should work no matter which line ending is in the database whether you are on Windows, Unix, or a Mac.

        If you are (as it looks) trying to define a set of conversions for turning text into somewhat equivalent HTML, you might also want to look at Parse::RecDescent for a much better way to do parsing than just throwing a few regular expressions at it. Or you can go for the hand-rolled version at Why I like functional programming (see the bottom of the thread for a cleaner rewrite through). The rule here could be done by adding the handler

        sub {'<br>'}
        for each of "\r\n", "\n\n", "\r", and "\n". Why would you do that you ask? Well because with that code you can readily implement markup rules based on user defined choices without getting into a mess of custom logic.
Re: Strange values in hash reference
by tachyon (Chancellor) on Aug 14, 2001 at 13:57 UTC

    Your logic seems OK. This simulates what you are doing and works fine:

    %hash = ('subjects' => "foo\n", 'bar' => "baz\n"); $job_hash = \%hash; print $job_hash, "\n"; # add this print $$job_hash{'subjects'}; # add this $$job_hash{'subjects'} =~ s/\n/<br>/g; print $$job_hash{'subjects'};

    To debug it I would add the two lines noted to ensure you have a hash ref and that the data is what you expect before you try to modify it. As the code above works fine (on a hash ref) this should help you identify the source of the problem.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Strange values in hash reference
by Aighearach (Initiate) on Aug 14, 2001 at 14:45 UTC
    I don't know if it's relevant here, but sometimes where there is a CR LF (\r\n) and I've translated out the \n, and not the \r, I've ended up with debug print()s with data "over the top" of the correct data.
    --
    Snazzy tagline here