Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^6: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff

by likbez (Sexton)
on Sep 15, 2020 at 15:12 UTC ( [id://11121799]=note: print w/replies, xml ) Need Help??


in reply to Re^5: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff
in thread What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff

It removes only leading whitespace. There is no alternative to regex idiom for trailing whitespace that I know of.
  • Comment on Re^6: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff

Replies are listed 'Best First'.
Re^7: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff
by Tux (Canon) on Sep 15, 2020 at 15:29 UTC

    No elegant alternative. As I showed in the benchmark, this works fine:

    sub splt{split" ",reverse((split" ",(reverse$x),1)[0]),1;};

    To be honest, i all the years that I "do" Perl (since perl-4.016), I have never seen anyone using split to trim leading (or trailing) whitespace.


    Enjoy, Have FUN! H.Merijn

      Had a reeeeeaaaaly vague recollection and the Perl Cookbook (Ch 1 section 19) does actually offer $_ = join(' ', split(' ')); as an alternative to these three substitutions:

      s/^\s+//; s/\s+$//; s/\s+/ /g;

      to strip and canonicalize to single spaces; and does offer this trim sub:

      sub trim { my @out = @_ ? @_ : $_; $_ = join(' ', split(' ')) for @out; return wantarray ? @out : "@out"; }

      Edit: That being said, I don't recall having seen this construct in the wild otherwise and had the vaguest of hunches that PC mentioned anything like this so I'd hardly call it a "common idiom" either.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        How often do you need to compress internal whitespace? Anyway. it is faster:

        $ perl -MBenchmark=cmpthese -wE'my$x=join" "=>"",("abc")x5,"";say"s +ourc: |$x|";sub trim{join" ",split" ",$x};sub rgx{$x=~s/^\s+//r=~s/\s ++$//r=~s/\s\s+/ /gr};say "split: |",trim(),"|";say"regex: |",rgx(),"| +";cmpthese(-2,{splt=>\&trim,rgx=>\&rgx})' sourc: | abc abc abc abc abc | split: |abc abc abc abc abc| regex: |abc abc abc abc abc| Rate rgx splt rgx 506423/s -- -71% splt 1767763/s 249% --

        Enjoy, Have FUN! H.Merijn
      No elegant alternative. As I showed in the benchmark, this works fine:

      sub splt{split" ",reverse((split" ",(reverse$x),1)[0]),1;};
      The most elegant approach to this problem is the use of tr function. The implementation below beats regex three times and can be made faster by trivial extension of tr function mentioned in my prev post (something like option 'x' -- stop the translation on the first symbol outside the set1 and return this position), which can be used instead of more general function index for searching single characters in the string and can made like rindex to be able to search in reverse direction too.

      Looks like the solution for trim from the Cookbook mentioned by Fletch along with potentially deforming the string is slower then regex in my test(on my machine it took 3.56 sec real time).

      Here is the "tr based" algorithm for trim:

      time perl -e 'for (1..1000000) { $line=" aaa bbb ccc ddd eee fff "; $_=$line; $_=~tr/ /x/c; $start=index($_,'x'); $line=substr($line,$start,rindex($_,'x')-$start+1); }'
      
      real 0m1.112s
      user 0m1.076s
      sys 0m0.031s
      
      Can be made into a single statement making it slightly( ~7%) slower:
      $line=substr($line,($start=index($_=$line=~tr/ /x/cr,'x')),rindex($_,'x')-$start+1);
      
      # time perl -e 'for (1..1000000) { $line=" aaa bbb ccc ddd eee fff "; $line=substr($line,($start=index($_=$line=~tr/ /x/cr,'x')),rindex($_,'x')-$start+1); }' 
      real 0m1.189s
      user 0m1.154s
      sys 0m0.015s
      
      
        or
        $line=substr($line,index($_=$line=~tr/ /x/cr,'x')),rindex($_,'x')-leng +th($line)+1);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11121799]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found