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

Re: To Split or Not to Split

by FunkyMonk (Chancellor)
on Apr 24, 2011 at 23:31 UTC ( [id://901115]=note: print w/replies, xml ) Need Help??


in reply to To Split or Not to Split

Your question is so vague I don't know if this will help you or not
Use split when you know what to throw away and a capturing regex when you know what to keep
I would love to attribute that quote to someone, but I've known it for so long I forgot who wrote it :(

update:

Following, tchrist's and Corion's comments I googled and I got the quote wrong, so I've added a word (s/regex/capturing regex/).

I also found this link where Dominus attributes the quote to merlyn.

I agree with all of tchrist's comments. Perhaps I should also add that it's a rule of thumb and not the law. It certainly helped me choose between using splits and captures when I was a Perl noob.

Replies are listed 'Best First'.
Re^2: To Split or Not to Split
by tchrist (Pilgrim) on Apr 25, 2011 at 02:18 UTC
    “Use split when you know what to throw away and a regex when you know what to keep.”

    Except that that doesn’t really work, considering that:

    1. Because split takes a regex as its own argument, you cannot oppose “split” with “regex”: wherever you have split, there too do you also have a regex. It’s like some sort of logic error.

    2. Sometimes split does not necessarily throw (all of) what it matches away.
      $str = "this here and that or those there and his or hers nor thee"; @words = split /\h*\b(and|nor|or)\b\h*/, $str;
    3. A regex doesn’t always return (all of) what it matches.
      $str = "fee=1 fie=2 foe=3 fum=4"; my %settings = $str =~ /\b(\w+)=(\S*)/g;
      and also, in a completely different way:
      % perl -pe 's//IoException/ if ?import java\.io\.\KFile?'

    So I would be careful with passing along that particular phrase. It’s catchy, but it isn’t really all that correct. Perhaps it was originally said about some other language than Perl, since it doesn’t seem to make sense for Perl when looked at closely.

    At most I might point out that m//g and split are often used in complementary senses, with one looking for the parts you’re interested in and the other for the parts you’re not. Even so, in many contexts I’d hasten to add that while they might kinda work that way in common cases, that’s much too simplistic a description for what you can — and quite often do — do with both of these constructs in Perl.

    Let’s just say that the refrain presents a rather simplified version of reality. :)

      Maybe "match" is a better term than "regex", but still, I also found that most of the time when I despaired trying to make split do my bidding, a match would easily collect the information I wanted (and knew how) to keep.

Re^2: To Split or Not to Split
by burningredmoon (Novice) on Apr 25, 2011 at 00:10 UTC
    lol. It's okay but thanks for replying!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found