Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Split on . (dot)

by fo0 (Novice)
on Oct 05, 2003 at 12:45 UTC ( [id://296690]=perlquestion: print w/replies, xml ) Need Help??

fo0 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i'm perl beginner, be indulgent :)   I'm writting a simple code just for use 'split'.
$ip=<STDIN>; @t=split(/./,$ip); $x=@t; print "$x\n"; print "@t\n";
Ok,i know split(/./,.. ')is wrong to match '.'   I don't understand why @t is empty, but nevertheless $x=@t; return me n elements ...
Ex:
foonikens@xxxx:~$ ./ipcalc.pl Entrez votre ip :21.23 6

An idea ?

janitored by ybiC: Retitle from "Array empty"

Replies are listed 'Best First'.
Re: Array empty
by barrd (Canon) on Oct 05, 2003 at 13:04 UTC
    Hi foO,
    If I understand you correctly you guessed right why @t is empty because you needed to escape the fullstop (dot ".") in split. Try something like this:
    #!/usr/bin/perl -w use strict; my $ip = "192.168.2.34"; my @t = split(/\./, $ip); my $x = @t; print "Number of elements in array: $x\n"; print "Content of array: @t\n";
    As a beginner now would be a good time to learn to use strict and warnings (-w flag after shebang). These are handy tools for debugging your future scripts.

    update: changed text slightly because the node got retitled.

Re: Array empty
by delirium (Chaplain) on Oct 05, 2003 at 14:31 UTC
    Two things that help in deducing what's going on with a split function are to tell perl to keep the delimiter matches as elements of the list it returns, and to print out the array line by line rather than with a print "@t\n";

    This is how I would attack the problem:

    #!/usr/bin/perl print "Entrez votre ip :"; $ip=<STDIN>; @t=split(/(.)/,$ip); # Surround the delimiter in (), then the # regexp matches become array elements print scalar @t,"\n"; # Don't waste a variable print "[$_]\n" for @t; # Surround each match by brackets, # print each match on its own line.

    Then your output becomes:

    Entrez votre ip :21.23 11 [] [2] [] [1] [] [.] [] [2] [] [3] [ ]

    "Oh, each byte was a match," says I. Changing @t=split(/(.)/,$ip); to @t=split(/(\.)/,$ip); gives:

    Entrez votre ip :21.23 3 [21] [.] [23 ]

    OK, good. Now we need to get rid of that line feed, and take the delimiter out of the array. Final code:

    #!/usr/bin/perl print "Entrez votre ip :"; $ip=<STDIN>; @t=split(/[.\n]/,$ip); # Don't need \. for period in [ ] print scalar @t,"\n"; print "[$_]\n" for @t;

    Which outputs:

    Entrez votre ip :21.23 2 [21] [23]

    Was that what you were looking for?

      To get rid of the newline you probably should use chomp instead of putting it in the list of delimiters.

      Makeshifts last the longest.

Re: Array empty
by arno (Scribe) on Oct 05, 2003 at 13:09 UTC
    hi, In you code, split returns all fields found in $ip using '.' metacharacter as a delimiter. '.' metacharacter matches almost any character but newline. As a consequence,@t will contain invisible characters as NUL character and \n. When you do :
    map { print ord $_,"\n" } split(/./, $ip);
    You 'll print the ascii code of each element in the returned list by split function. Here's the output i get :
    0 
    0
    0
    0
    0
    10
    0 is the ascii NUL and 10 is the newline character. I hope you understand these explanations, Arnaud
      Close. But ord($_) returns 0 not because it contains a null byte, but because it is an empty string.
      print ord("");
      prints: 0
      (Where on earth would he be getting null bytes from?)

      Indeed, the resulting array he's getting after doing

      $ip = "21.23\n"; @t = split /./, $ip;
      is
      ("", "", "", "", "", "\n")
      

      The explanation: every character in the input string, except the newline, is used as a split delimiter. Thus you get, as a result, the stuff between those characters, which is nothing every time, except at the last character.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-16 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found