Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: does split(".") work?

by Joost (Canon)
on Nov 15, 2009 at 23:23 UTC ( #807329=note: print w/ replies, xml ) Need Help??


in reply to does split(".") work?

split does not split on a string but on a regular expression. Dot "." is the "match-all" character in a regex so your split won't return anything.

You want my @values = split /\./, $ip instead.


Comment on Re: does split(".") work?
Download Code
Re^2: does split(".") work?
by jwkrahn (Monsignor) on Nov 16, 2009 at 00:23 UTC
    Dot "." is the "match-all" character in a regex so your split won't return anything.

    Actually split returns a lot of things, it's just that the way list assignment works there is nothing left to put in the array.    If you make a slight change you can see everything that split returns:

    $ perl -le'my $ip="158.21.17.45"; my @values = split(".", $ip, -1); pr +int $ip,"\t\t",$#values,"\n";' 158.21.17.45 12
      Not sure what you mean about list assignment; the difference is that a third parameter of -1 makes split keep trailing empty strings where by default it discards them. In your example, split returns 13 empty strings (one from before the first character of $ip, one for each position between characters, and one from after the last character.)
      --
      A math joke: r = | |csc(θ)|+|sec(θ)|-||csc(θ)|-|sec(θ)|| |
      Online Fortune Cookie Search
      Office Space merchandise

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (16)
As of 2015-07-06 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (74 votes), past polls