Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do you enter text before and after each character in a string in Perl?

How do you enter text before and after each character in a string in perl?

for example: The string to be inputted, BEFORE the each char is "1", and AFTER each character is "2".

The string entered is : "A big dog"

Result should be : 1A2 1b21i21g2 1d21o21g2

4 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Your example result does not put the text before spaces, so you'd need to change howlin_mouse's solution to look at non-space characters. Otherwise you'd have 1A21 21b21i21g21 21d21o21g2. Actually, I think his answer would give you 1A big dog2 since you're looking for one or more characters, but I'm not at a computer with perl to test it out.

  • Lisa A
    Lv 7
    1 decade ago

    You don't "enter" it at all. Just construct a completely new string. Break the entered string into characters, and add each character and the separators to the new string.

  • ?
    Lv 4
    5 years ago

    in case you recommend you like loop over each and each letter of each and every line, something like on a similar time as($line = record) { @letters = split //, $line; foreach $l ( @letters ) { print $l; } } or something comparable.

  • 1 decade ago

    use a regex. string ~= s/(.)+/1$12/g

Still have questions? Get your answers by asking now.