Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
biznatchio
Mar 31, 2001


Buglord
Perl diving headfirst into the Osborne Effect and then sleeping for a decade certainly didn't help, but I agree that the core reason Python and Ruby took over is because they have easier to read syntax. TMTOWTDI is a great philosophy until you discover it means to understand everyone else's code that you need to know every dark corner of the language; and that's a huge drag on adoption in every niche outside of single-maintainer/one-off scripts.

Adbot
ADBOT LOVES YOU

biznatchio
Mar 31, 2001


Buglord

Hughmoris posted:

Rookie perl question:

Given a string like '192.168.0.1:21,22,80,445,1432,30220' what would be a slick perl way to check if port 22 is in that string?

I can write it the naive way but I'm curious about a short-hand way to do it.

An example

To explain for a perl rookie, the check is:

pre:
if ($var =~ /(:|,)22(,|$)/) {
The =~ operator tests if a string matches a regex pattern. The regex is /(:|,)22(,|$)/ which tests if the string contains:

  • Either the character : or , -- followed by
  • The digits 22 -- followed by
  • Either the character , or the end of the string

biznatchio fucked around with this message at 04:33 on Apr 15, 2024

biznatchio
Mar 31, 2001


Buglord
In my experience, the only time you ever need to worry about regex performance is if you have backtracking; and even then only when you fall into a catastrophic backtracking case that generally only happens when you stack multiple variable length operators on top of each other.

Neither of the provided regexes use a concerning backtracking pattern, so their performance differences are almost certainly immaterial, even across 1GB of data. From a performance perspective, Knuth's quote on the matter applies here:

quote:

The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.

Choose one or the other based on readability; because in this case you're going to spend more wallclock time looking at the pattern as the developer than the CPU will spend actually executing it.

(And it is worth pointing out that neither pattern will work correctly if the strings might ever contain IPv6 addresses; some additional pattern work would be necessary to distinguish colons in the address from the colon separating the address from the port list.)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply