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

(Quiz) To split, or not to split?

by japhy (Canon)
on Nov 05, 2001 at 07:46 UTC ( #123269=perlmeditation: print w/ replies, xml ) Need Help??

You have a string in $_ and you want to get a list of the characters in it. You decide to use @chars = split //, but then your friend (who happens to like regexes a lot) advertises the code @chars = /./gs stating that it works the same way as the other code. You run it on all the possible strings you can come up with, and indeed, it returns the exact same value. But you can't help but thinking it does something different. What is it? Please keep answers encoded, whether it be via ROT-13 or font coloring.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Comment on (Quiz) To split, or not to split?
Re: (Quiz) To split, or not to split?
by chipmunk (Parson) on Nov 05, 2001 at 08:12 UTC
    Tricky!

    @chars = /./gs; grabs characters starting at pos(), and resets pos() afterwards.

    For example:

    #!perl $_ = "foo"; @chars1 = split //; @chars2 = /./gs; /f/g; @chars3 = /./gs; print "@chars1\n@chars2\n@chars3\n";

Re: (Quiz) To split, or not to split?
by blakem (Monsignor) on Nov 05, 2001 at 08:18 UTC
    Just a guess...

     

    Mucking around with pos() beforehand makes them behave differently.

    #!/usr/bin/perl -wT use strict; $_ = 'abcdefg'; pos($_) = 3; # <= this modifies the regex but not the split my @split = split //; my @regex = /./gs; print "S:$_\n" for @split; print "\n"; print "R:$_\n" for @regex; =OUTPUT S:a S:b S:c S:d S:e S:f S:g R:d R:e R:f R:g

    -Blake

Re: (Quiz) To split, or not to split?
by dws (Chancellor) on Nov 05, 2001 at 08:29 UTC
    split // ignores leading whitespace. The regexp doesn't. " foo" will yield different results.
    Wrong. But there is a (small) wording ambiguity in the description of split in perlfunc.

Re: (Quiz) To split, or not to split?
by japhy (Canon) on Nov 05, 2001 at 09:06 UTC
    Well, I got two correct answers (regarding pos()) that I wasn't expecting! The answer I had in mind was the using split() will not reset your $DIGIT variables, whereas use of another regex will. But again, I'm glad you guys found something that didn't occur to me. Both of these points are going into my book, by the way.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Yet another answer (or rather a precision to japhy's solution)
      Not only the $DIGIT variables are reset, also the infamous $& $' $` and the @+ and @- arrays. That's $MATCH, $PREMATCH, $POSTMATCH, @LAST_MATCH_END and @LAST_MATCH_START for those who use English

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://123269]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-01 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls