in reply to replace text using hash

In addition to what LanX says, a couple comments: So, my solution (untested) would likely look like:
my $re = join '|', map "\Q$_\E", sort {length $b <=> length $a} keys % +dictionary; while (<FILEIN>) { s/($re)/$dictionary{$1}/g }
This will not fix the whitespace issue. To do that, you'd probably need to slurp the file, modify the regex to treat whitespace agnostically, and then replicate the type of whitespace in your result. Doing this is probably a giant pain.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: replace text using hash
by LanX (Saint) on Sep 08, 2014 at 18:10 UTC
    > modify the regex to treat whitespace agnostically, and then replicate the type of whitespace in your result.

    OK I missed that part. But I think substituting each blank with a grouped (\s) should do.

    Like this the positions of each whitespace match is known and can be mapped back to blank before doing the hash look-up.

    Evidently using any whitespace different to blank must be forbidden in the keys.

    And at this point it gets obvious that looking at CPAN for a ready to use module should be a good idea! :)

    update

    In hindsight thats BS. This kind of translation with different length expressions can't reproduce good formatting.

    It's better to hold each paragraph in one line in a normalized form, where each non-blank whitespace is eliminated and to do the formatting again after substitution .

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re^2: replace text using hash
by LanX (Saint) on Sep 08, 2014 at 17:29 UTC
    > One feature of LanX's solution that was not mentioned

    true, I was too lazy to type more... ;-)

    > you'd probably need to slurp the file

    I think paragraphs should be enough. And either the /s or /m modifier should help with newline as whitespace. (still need a mnemonic but I think it was /m /s because it was counter-intuitive)

    update

    seems like \s already matches newline as whitespace. The /s modifier only effects the match-all-dot . to match newlines.

    So replacing all whitespaces in the keys with \s+ should be sufficient.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re^2: replace text using hash
by Anonymous Monk on Sep 09, 2014 at 13:33 UTC

    Hi,

    Thank you for your replies. I have done it but the output is:

    This is a ARRAY(0xe5d6c0)

    Thanks

      See update of my first reply.

      The whole push is nonsense.

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        So, what can I do in that case?