Splitting on a period in Windows.

N

nub5

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

Gunnar Hjalmarsson

nub5 said:
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.
 
E

Eric Schwartz

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
 
B

Bob Walton

nub5 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top