Splitting on a period in Windows.

Discussion in 'Perl Misc' started by nub5, Apr 9, 2004.

  1. nub5

    nub5 Guest

    Hey Folks I was hoping someone could explain this to me. Normally I
    used perl in unix and I do the following command.

    $filename = "data.dat";
    @temp =split("\.","$filename");

    For some reason this does not work with Perl on Windows. However if I
    do the following change from " to / it works.

    $filename = "data.dat";
    @temp =split(/\./,"$filename");

    Why is that? Can anyone provide me with an explanation. Should I not
    use " and stick with /'s or the q's? I'm going to be writing a lot of
    cross-platform (unix, windows) code.
    nub5, Apr 9, 2004
    #1
    1. Advertising

  2. nub5 wrote:
    > Hey Folks I was hoping someone could explain this to me. Normally
    > I used perl in unix and I do the following command.
    >
    > $filename = "data.dat";
    > @temp =split("\.","$filename");
    >
    > For some reason this does not work with Perl on Windows. However
    > if I do the following change from " to / it works.
    >
    > $filename = "data.dat";
    > @temp =split(/\./,"$filename");
    >
    > Why is that? Can anyone provide me with an explanation. Should I
    > not use " and stick with /'s or the q's?


    Wonder what the docs for the split() function says. Hmm...

    > I'm going to be writing a lot of cross-platform (unix, windows)
    > code.


    Then it's reasonably a good idea to not rely on the 'trial-and-error'
    method, but rather follow the documented syntax.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 9, 2004
    #2
    1. Advertising

  3. (nub5) writes:
    > Hey Folks I was hoping someone could explain this to me. Normally I
    > used perl in unix and I do the following command.
    >
    > $filename = "data.dat";
    > @temp =split("\.","$filename");


    Well, that's your problem right there, most likely. If you read the
    documentation for the 'split' function with 'perldoc -f split', you
    will see that the first parameter is a regex, not a string. I'm just
    about to leave, so I'm not going to figure out why this works, but
    it's quite possible it's pure luck that it works for you.

    I expect someone will prove that last sentence to be pure balderdash,
    so I'm not standing behind it very strongly. :)

    > For some reason this does not work with Perl on Windows. However if I
    > do the following change from " to / it works.
    >
    > $filename = "data.dat";
    > @temp =split(/\./,"$filename");
    >
    > Why is that? Can anyone provide me with an explanation.


    Perl itself can provide you with an explanation, if you read the
    standard documentation included with your Perl distribution. I say
    this not to be a sanctimonious git, but to help you not offend too
    many other people here, who are very knowledgable about Perl and could
    be a lot of help to you if you let them: please try not to ask
    questions here that you could answer in about 20 seconds on your own.

    If you have a question about how a function works, use the command

    perldoc -f <function>

    on the command line.

    For a module's documentation, use

    perldoc <module>

    You should also familiarize yourself with the Perl FAQ. 'perldoc
    perltoc' will give you the headings of the sections and what questions
    are answered in them. If you always check there first, to make sure
    your question isn't answered in the FAQ (or any of the other resources
    I mentioned), you stand a much better chance of getting answers more
    verbose than "read the documentation".

    > Should I not use " and stick with /'s or the q's? I'm going to be
    > writing a lot of cross-platform (unix, windows) code.


    You should also familiarize yourself with the perlport manpage ('man
    perlport' on UNIX, or 'perldoc perlport' everywhere). But no matter
    what kind of code you write, learning to consult the documentation on
    your hard drive first will save you lots of time (USENET propogation
    is not consistent or necessarily even complete), and will save us lots
    of frustration. A win for everyone, I think.

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Apr 9, 2004
    #3
  4. nub5

    Bob Walton Guest

    nub5 wrote:

    > Hey Folks I was hoping someone could explain this to me. Normally I
    > used perl in unix and I do the following command.
    >
    > $filename = "data.dat";
    > @temp =split("\.","$filename");
    >
    > For some reason this does not work with Perl on Windows. However if I
    > do the following change from " to / it works.
    >
    > $filename = "data.dat";
    > @temp =split(/\./,"$filename");
    >
    > Why is that? Can anyone provide me with an explanation. Should I not
    > use " and stick with /'s or the q's? I'm going to be writing a lot of
    > cross-platform (unix, windows) code.



    That is because split() takes (except for one special case) a regexp as
    its first argument. You provided it with a string, specifically the
    string . after removal of the \-quoting in the "-delimited string. That
    string then gets converted to a regexp by split. The . is a regexp
    metacharacter which will match any character. So you will split on
    every character of your string, which, while it *does* "work", doesn't
    do what you want to do. When you supply /\./ to split, split gets a
    genuine regexp that will match a literal . character. See:

    perldoc -f split

    That will not be a problem on just Windoze -- Perl on any platform will
    do the same thing. I don't believe you that it worked differently under
    Unix (it doesn't when I try it). Maybe it worked differently under some
    ancient version of Perl a long long time ago.

    So in summary: "\." is the same as '.' which goes to /./ in split
    and "\\." is the same as '\.' (or '\\.') which goes to /\./
    so you might as well just use what you intended, which is /\./ .

    Also note that you have expressions like "$var" in your code. For why
    that is bad, see:

    perldoc -q quoting

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Apr 9, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page