Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

does split(".") work?

by dtd1941 (Initiate)
on Nov 15, 2009 at 23:06 UTC ( #807325=perlquestion: print w/ replies, xml ) Need Help??
dtd1941 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks:

I figure this must be my error, but I can not find it.

#!/usr/bin/perl use strict; use warnings; my $ip=""; my @values = split(".", $ip); print $ip,"\t\t",$#values,"\n"; exit(0); ----running the above---- ./ -1

Comment on does split(".") work?
Download Code
Re: does split(".") work?
by Joost (Canon) on Nov 15, 2009 at 23: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=""; my @values = split(".", $ip, -1); pr +int $ip,"\t\t",$#values,"\n";' 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
Re: does split(".") work?
by biohisham (Priest) on Nov 15, 2009 at 23:25 UTC
    use strict; use warnings; my $ip=""; my @values = split(/\./, $ip); #<----Notice the Regex. print $ip,"\t\t",$#values,"\n"; for my $val (@values){ print "$val\n"; } exit(0);
    OUTPUT 3 158 21 17 45
    Note:, Coincided with Joost's reply, we replied at the same time.

    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: does split(".") work?
by ikegami (Pope) on Nov 16, 2009 at 00:05 UTC
    [ This is a different spin on the same answer. ]

    split's first argument isn't matched literally. It's treated as a regex pattern. The regex pattern can be provided as any of the following:

    • a string,
    • a compiled pattern
    • a match operator (which passes a compiled pattern)

    The following are equivalent:

    my @octets = split('\\.', $ip); my @octets = split(qr/\./, $ip); my @octets = split(/\./, $ip);

    I always use the last syntax. It avoids confusion.

    You might also be interested in Socket's inet_aton (and inet_ntoa).

    (Note that split ' ' is special. See split's documentation if you're interested in details.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://807325]
Approved by biohisham
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2015-01-31 21:56 GMT
Find Nodes?
    Voting Booth?

    My top resolution in 2015 is:

    Results (261 votes), past polls