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

Hello Monks, Like I said I'm a newbie. I would like to find some help. My requirement is: I have a variable with text like
#!/usr/bin/perl -w use warnings; use strict; use Spreadsheet::Read 'ReadData'; use Encode 'decode'; use XML::LibXML; use Text::FixEOL; no warnings; my $INFILE = 'TestPGR.xlsx'; my $ENCODING = 'MacRoman'; my $OUTFILE = 'TestPGR.xml'; my %FIELDS = ( 1=>'docid', 2=>'title', 3=>'version', 4=>'revision', 5=>'node_order', 6=>'description', 7=>'status', 8=>'type', 9=>'expected_coverage', ); my $text = "Sur toutes les ressources « Usine » on suivra les valeurs +suivantes : • Temps de préparation machine (Heures machine des Arrêts programmés)" +;
I would like to learn to loop on my variable and convert end of line with HTML tag like my $text ="Sur toutes les ressources « Usine » on suivra les valeurs suivantes :
• Temps de préparation machine (Heures machine des Arrêts programmés)"; I have tried with HTML::Parser without any success, someone can give me some help to find an approach ? Thanks for all, Bala

Replies are listed 'Best First'.
Re: Newbie - convert carriage return in a variable
by hippo (Archbishop) on Feb 17, 2019 at 16:44 UTC
    I have tried with HTML::Parser

    I wonder why, since there is no HTML to parse. Instead see this solution using s///:

    use strict; use warnings; use Test::More tests => 1; my $have = 'foo bar'; my $want = 'foo<br />bar'; $have =~ s/\n/<br \/>/; is $have, $want;

    Hopefully you will see here that I have included nothing which is superfluous to the task at hand.

      Thanks hippo, In this case the script will just replace the first end of line found. If I want to replace more than one as
      use strict; use warnings; use Test::More tests => 1; my $have = 'foo bar'; my $want = 'foo<br /><br />bar'; $have =~ s/\n/<br \/>/; is $have, $want;
      I think I need to us chomp function but It's not clear how to use it.
      use strict; use warnings; use Test::More tests => 1; my $have = 'foo bar'; my $want = 'foo<br /><br />bar'; $have =~ s/\n/<br \/>/; is $have, $want; my @data=split(/\n/<br \/>/); foreach my $have (@data) { $want = $have; }
      But it's not working :(

        Add the g modifier to your regex

        #!perl use strict; my $text = 'foo bar'; $text =~ s/\n/<br \/>/g; print $text;
        poj