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

Dear Monks,

I have a file .txt where I write the name of a directory and then a bunch of letter preceded of a "/". The content of my file looks like:
C:\Documents and Settings\Claire\Desktop\ /LOG:fgf /L /XF doc /W:3 /R:10 /COPY:dtao /FP
Now, I do the following:
open (IN, "<command.txt"); while(<IN>){ @array = split '/'; }#while close IN;
and the first element of my array is: /C:\Documents and Settings\Claire\Desktop\. Why is that? There is no "/" in front of "C:" in my file!!! Anybody would like to tell me?
Thanks in advance for your time and help,
Claire

Replies are listed 'Best First'.
Re: "/" with directory in an array
by pg (Canon) on Aug 14, 2005 at 00:39 UTC

    There must be something in your code that is doing this, but you didn't show us that part. What you showed us should work fine:

    while(<DATA>){ @array = split '/'; print $array[0]; } __DATA__ C:\Documents and Settings\Claire\Desktop\ /LOG:fgf /L /XF doc /W:3 /R: +10 /COPY:dtao /FP

    This gives me "C:\Documents and Settings\Claire\Desktop\".

Re: "/" with directory in an array
by sk (Curate) on Aug 14, 2005 at 00:43 UTC
    I am not seeing that on my machine

    #!/usr/bin/perl -w while(<DATA>){ @array = split '/'; print $array[0],$/; } __DATA__ C:\Documents and Settings\Claire\Desktop\ /LOG:fgf /L /XF doc /W:3 /R: +10 /COPY:dtao /FP

    Output

    C:\Documents and Settings\Claire\Desktop\

    I modified your IN to DATA but it should not cause any issues. Actually i tested this code using the contents inside command.txt and i got the same output

    Can you print out the entire line from the file before you split it? like  print;

    Also just so that you know split's first arg is a regular exp

    I would have written  split /\//

    -SK

Re: "/" with directory in an array
by graff (Chancellor) on Aug 14, 2005 at 17:25 UTC
    As pg pointed out, there is something you aren't telling us, and the part you left out is bound to be the part that causes the problem.

    How do you know that there is a "/" at the beginning of the first element of your array? The OP code doesn't show anything being done with the contents of @array. Show us the part of the code where you get to see the "/".

A reply falls below the community's threshold of quality. You may see it by logging in.