Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

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


in reply to Re: 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

This node falls below the community's threshold of quality. You may see it by logging in.

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

    Perl has the s/// operator to trim leading or trailing whitespace:

    $line =~ s/^\s+//; # ltrim() $line =~ s/\s+$//; # rtrim()

    This is as short as ltrim and already exists.

    Maybe you want to learn about Perl before suggesting new features? For example, you don't seem to know about wantarray and how you can use it and the argument aliasing to implement

    ltrim($line,7); # and $line2 = ltrim($line,7);

    Yes, if you want to pass around regular expressions, you need to use the qr// quotes (not single quotes please), but that's a minor price to pay to get a feature right now instead of waiting for somebody to implement it.

      Yes I did not know about wantarray. Sorry about that. That can be used to distinguish between function and sub calls. Thank you.
      wantarray

      Returns true if the context of the currently executing subroutine or eval is looking for a list value. Returns false if the context is looking for a scalar. Returns the undefined value if the context is looking for no value (void context).

      FYI instead of $line =~ s/\s+$//; # rtrim() in Perl you can use AWK hack in split function:

      ($line)=split(' ',$line,1)
      It is marginally faster. Use of regex for trimming white space reminds me an anecdote about general who sent a tank division to capture an unarmed village with natives. IMHO, this is too heavy instrument unless regex engine is really sophisticated and optimize such case into tr/ //d style implementation. Just look at the overhead on a million lines file:

      [0] # time perl -e 'for (1..1000000) { $line=" aaa bbb ccc ddd eee +fff "; $line =~ s/\s+$//; $line =~ s/\s+$//;}' real 0m3.526s user 0m3.510s sys 0m0.000s

      so we are talking about real money here (without regex time is 0.14).

      May be extending tr to stop after the first symbol which is not in set1 would also be beneficial and faster.

      In my use of Perl I would prefer an option in the open statement that would do the job for all lines read, as this is the most common use case. Something like open(file,'<t(rln)','test')

        FYI instead of $line =~ s/\s+$//; # rtrim() in Perl you can use AWK hack is split:

        ($line)=split(' ',$line,1)

        Perhaps I am misunderstanding something but are you saying that $line =~ s/\s+$//; and ($line)=split(' ',$line,1); are equivalent? That doesn't seem to be the case, the latter appearing to implement the ltrim() you desire. This code

        use strict; use warnings; my $line = q{ aaa bbb ccc ddd eee fff }; ( $line ) = split( ' ', $line, 1 ); print qq{-->$line<--\n};

        produces

        -->aaa bbb ccc ddd eee fff <--

        Perhaps you could clarify?

        Cheers,

        JohnGG

        Please also show the time of just populating the lines with no substitution.
        |14:29 choroba@triangle |~ $ time perl -e 'for (1..1000000) { $line=" aaa bbb ccc ddd eee ff +f ";}' real 0m0.041s user 0m0.042s sys 0m0.000s |14:29 choroba@triangle |~ $ time perl -e 'for (1..1000000) { $line=" aaa bbb ccc ddd eee ff +f "; $line =~ s/^ +//; $line =~ s/ +$//;}' real 0m0.747s user 0m0.740s sys 0m0.004s
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^3: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff
by GrandFather (Saint) on Sep 15, 2020 at 06:38 UTC
    One argument in favor of such functions is that in many languages the elimination of free space at the beginning and end of strings is recognized as an important special case and built-in function provided for this purpose. Perl is one of the few in which there is no such special operation.

    What are these "many languages" you speak of? Of the dozen of so languages I've used and can remember only a couple (probably only one) have some form of trim function. Rather than being "one of the few", Perl is in the vast majority in not providing a built in explicit trim function.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      PHP, Javascript, Ruby, Julia and Python come to mind. In R you can use trimws() and set strip.white=TRUE for reading files.
Re^3: What esteemed monks think about changes necessary/desirable in Perl 7 outside of OO staff
by ikegami (Patriarch) on Sep 16, 2020 at 17:10 UTC

    heh? No, there's nothing special needed here. There would be absolutely no loss of flexibility.

    The key question here is not whether "you can do it"

    You are sorely mistaken.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found