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

I have this code in my page print $query->p ("iCAD Detail   : $Detail\n"); and this is stored in $Detail variable:
'Sep 30 2016 8:32AM - [2] [Notification] [CHP]-Problem changed from IN +FO-Information to CLOSURE of a Road by CHP\nSep 30 2016 8:32AM - [1] +#2 NB LN CLOSED FROM EL RIVINO TO SANTA ANA\n'
As you can see it's a mess and very unreadable. Now my question is how do I format it better so it does a newline right before it display the date? something like this:
Sep 30 2016 8:32AM - [2] [Notification] [CHP]-Problem changed from INF +O-Information to CLOSURE of a Road by CHP\n
Sep 30 2016 8:32AM - [1] #2 NB LN CLOSED FROM EL RIVINO TO SANTA ANA\n +'
The values in $Detail are copied form a database entry.

Replies are listed 'Best First'.
Re: Print a big string formatted and in multiple lines
by haukex (Archbishop) on Sep 30, 2016 at 17:51 UTC

    Hi alen129,

    Please see How do I post a question effectively? - please use <code> tags to format not only your code but your data as well.

    To convert the string '\n' (that is, a literal backslash followed by the letter n) to a newline (perhaps plus something else), you could do

    $Detail =~ s/\\n/\n/g; # or $Detail =~ s/\\n/<br>\n/g; # for HTML

    Hope this helps,
    -- Hauke D

      Hauke D, The second line did it for me. Thank you so much. where can I learn more about this sort of string formatting?(what keyword do I search for?)

        Hi alen129,

        s/\\n/<br>\n/g is a search-and-replace regular expression, a good place to start is perlretut and perlrequick.

        Interpolation ('\n' vs. "\n" vs. "\\n") is discussed in a few different places, for example a bit in Scalar value constructors and Quote and Quote like Operators.

        Also, as mentioned as one of the points in the Basic debugging checklist, it's probably a good habit to get into to use a module like Data::Dumper or Data::Dump to look at the content your variables:

        my $Detail = 'Sep 30 2016 8:32AM CHP\nSep 30 2016 8:32AM SANTA ANA\n'; use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper $Detail; # - or - use Data::Dump 'dump'; print dump $Detail;

        Both will unambiguously show you the content of the string in Perl's syntax, for example "Sep 30 2016 8:32AM CHP\\nSep 30 2016 8:32AM SANTA ANA\\n".

        Hope this helps,
        -- Hauke D