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

Re: How to take the last character out of a string

by perlsen (Chaplain)
on Feb 17, 2005 at 03:59 UTC ( [id://431829]=note: print w/replies, xml ) Need Help??


in reply to How to take the last character out of a string

Hi, just try this,

$query1 = "select foo,bar,baz,"; $query1 =~ s#(.*),$#$1#gs; print "$query1"; #output #select foo,bar,baz

Replies are listed 'Best First'.
Re^2: How to take the last character out of a string
by Tanktalus (Canon) on Feb 17, 2005 at 04:20 UTC

    No doubt, this works. But it's doing a lot of work it doesn't need to.

    • Copying the whole string around. You're matching the whole string, except for the terminating comma, and then copying it back into $query1.
    • And then you're going to search the string again, presumably in case there is a second ending comma.
    • And what is the 's' for? You may have a habit of using it, thinking it solves a lot of your problems. I think you might be cultivating a bad habit - perl probably does things as defaults that are good things, and you want to specify overriding behaviour only when you need it. (Minimalist theory.)

    For the first one, just get rid of the (.*) and $1: $query1 =~ s#,$##gs. For the second, just add an appropriate plus sign: $query =~ s#,+$##s. For the last, well, just remove the ending s - it's not doing anything: $query =~ s#,+$##.

    "Premature optimisation is the root of all evil." Well, I'm not optimising - I'm merely encouraging idiomatic use of perl: writing perl the way it should be. Minimally, but not unreadably. (The last part doesn't apply to obfu's and golf competitions.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found