Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

My split command isnt working - please advise

by Angharad (Pilgrim)
on May 23, 2011 at 12:04 UTC ( #906284=perlquestion: print w/replies, xml ) Need Help??

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: etc
and I want to split the contents of each line on the period so that for:
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. 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


    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.)


    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__
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; ?


    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. - - [23/May/2011:19:02:56 +0000] "GET / HTTP/1.1" 200 9 +6 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; + +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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2022-06-25 02:14 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (81 votes). Check out past polls.