in reply to How to split, join and trim leading / trailing white space

G'day thanos1983,

"Is it possible to be done in one step?"

If you're using Perl 5.14, or later, you can chain those operations using the 'r' modifier. See "perl5140delta: Non-destructive substitution".

It's somewhat unclear what you're actually trying to achieve here. The use of Chinese characters seems superfluous to the actual question asked. The use of the 'g' modifier on the substitution, together with the '^' and '$' assertions, makes me wonder if you're perhaps dealing with multiline strings; however, the absence of the 'm' modifier suggests otherwise.

Here's some guesses as to the type of thing you might want:

$ perl -Mutf8 -C -E 'say join " ", split //, "北亰"'
北 亰

$ perl -Mutf8 -C -E 'say join " ", split //, " 北亰 " =~ s/^\s+|\s+$//r'
北 亰
$ perl -E 'say join(" ", split /(..)/, "e58c97e4bab0")' e5 8c 97 e4 ba b0 $ perl -E 'say join(" ", split /(..)/, "e58c97e4bab0") =~ s/^\s+|\s+$/ +/r' e5 8c 97 e4 ba b0

If you're simply unfamiliar with what's going on with split, that's explained at the end of that documentation: "If the PATTERN contains capturing groups, ...".

$ perl -E 'my @x = split /(..)/, "1234"; say "|$_|" for @x' || |12| || |34| $ perl -E 'my $x = join "_", split /(..)/, "1234"; say $x' _12__34

Update (additional information): As an additional example, to extend that last chaining example, you could do this to reduce multiple embedded spaces to a single space:

$ perl -E 'say join(" ", split /(..)/, "e58c97e4bab0") =~ s/^\s+|\s+$/ +/r =~ y/ / /rs' e5 8c 97 e4 ba b0

See "perlop: y/SEARCHLIST/REPLACEMENTLIST/cdsr" for more about that.

Update (further discussion): See my subsequent response (below) for further discussion and "some clarifications and corrections".

— Ken

Replies are listed 'Best First'.
Re^2: How to split, join and trim leading / leading white space
by thanos1983 (Parson) on Sep 06, 2017 at 08:28 UTC

    Hello kcott,

    That is perfect, thanks a lot for your time and effort. :)

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      "That is perfect, ..."

      Well, not quite! :-)

      I saw your meditation after I read, and responded to, your OP in this thread. I now see where the Chinese characters come from; although, I still think they're superflous in the context of this specific question.

      The focus of my answer was the 'r' modifier (in response to your "possible ... in one step?"). I probably should have paid more attention to your regex (/^\s+|\s+$/), rather than just copying it verbatim. With the Chinese issue out of the way, and having spent some time looking more closely at what I wrote, here's some clarifications and corrections.

      The substitution example with the Chinese characters should have included a 'g' modifier. I'm now reasonably certain that wasn't what you wanted; however, it should have been written like this:

      $ perl -Mutf8 -C -E 'say join " ", split //, " 北亰 " =~ s/^\s+|\s+$//gr'
      北 亰
      

      I was correct in not using the 'g' modifier in the other two substitution examples; however, I should have also removed the alternation. As the two examples splitting "1234" clearly demonstrate, there's no trailing whitespace: you only need to remove the leading whitespace. For those examples, these would have been better:

      $ perl -E 'say join(" ", split /(..)/, "e58c97e4bab0") =~ s/^\s+//r' e5 8c 97 e4 ba b0 $ perl -E 'say join(" ", split /(..)/, "e58c97e4bab0") =~ s/^\s+//r =~ + y/ / /rs' e5 8c 97 e4 ba b0

      Now, hopefully, it's "perfect". :-)

      — Ken

        say join(" ", split /(..)/, "e58c97e4bab0") =~ s/^\s+//r =~ y/ / /rs;
        Augh! No! split /(..)/ makes a list with empty strings in it that you don't want. Those result in extra spaces when you join, which you then have to mop up with silly regexes. Use one of these instead:
        say join(" ", "e58c97e4bab0" =~ /(..)/g); say join(" ", unpack "(a2)*", "e58c97e4bab0");
          A reply falls below the community's threshold of quality. You may see it by logging in.