http://qs321.pair.com?node_id=906284

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

Hi there

This is an embarassingly easy question I'm sure, but I simply dont know why my wee script isnt working properly.

I have a text file that looks like this:
1.2.56.3 3.55.7.3 etc
and I want to split the contents of each line on the period so that for:
1.2.56.3
I get
$nums[0] = 1, $nums[1] = 2 $nums[3] = 56 etc
Could anyone please point out whatever silly error is in my script? At the moment $nums[0] when printed contains the enire contents of the line e.g. 1.2.56.3 and all the others are (obviously) empty
#!/usr/bin/env perl use strict; my $file = shift; open(FILE, $file) || die "Unable to open $file for reading: $!\n"; while(<FILE>) { my @sf = split; #print "@sf\n"; print test my @nums = split(/\\./, $sf[0]); #print "@nums\n"; print "$nums[0]\n"; }
All help much appreciated!

Replies are listed 'Best First'.
Re: My split command isnt working - please advise
by FunkyMonk (Chancellor) on May 23, 2011 at 12:08 UTC
    You need a single backslash in your split, not two:

    my @nums = split(/\./, $sf[0]);

Re: My split command isnt working - please advise
by BrowserUk (Patriarch) on May 23, 2011 at 12:10 UTC

    This:my @nums = split(/\\./, $sf[0]); is attempting to split the string on the text \.

    Ie. You've escaped the backslash, so it is taken as a single literal and the dot will match any character.

    Change that to: my @nums = split(/\./, $sf[0]);

    That is escape the dot '.' with a (single) backslash so that the dot is taken as a literal.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: My split command isnt working - please advise
by roboticus (Chancellor) on May 23, 2011 at 12:11 UTC

    Angharad:

    Remove a backslash. It looks like your split command is trying to split on '\' + any character instead of '.'. (In other words, rather than escaping the period to make it a literal, you're escaping the backslash instead of the period.)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: My split command isnt working - please advise
by Angharad (Pilgrim) on May 23, 2011 at 12:17 UTC
    Thanks everyone!
Re: My split command isnt working - please advise
by wfsp (Abbot) on May 23, 2011 at 14:32 UTC
    Given the data you've shown I'm sure not you need the first split (check for what split uses as a default).
    #!/usr/bin/perl #use v5.12.2; use strict; use warnings; while(my $line = <DATA>){ chomp $line; my @nums = split(/\./, $line); print "@nums\n"; } __DATA__ 1.2.56.3 3.55.7.3
Re: My split command isnt working - please advise
by CountZero (Bishop) on May 23, 2011 at 17:34 UTC
    What are you trying to accomplish with the my @sf = split; ?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      It could be for splitting off the first word from a web server access log, where lines could look like eg.

      95.108.157.251 - - [23/May/2011:19:02:56 +0000] "GET / HTTP/1.1" 200 9 +6 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bot +s)"
      Though in that case, splitting on a dot might not be a good idea, especially because the first field could also be an ipv6 address, such as 2001:738:2001:2010:0:ff:fe00:3.