-
PERL: Simple Problem
:confused: whenever i use the backticks to send a command to the commandline i will not work if there is a space.
Code:
@files = `dir\\*.*`;
but
[code]@files=`dir d:\\Perl\\*.*`;[\code]won't.
and obviously
[code]@files=`dird:\\Perl\\*.*`;[\code] doesn't either. Any suggestions?
In case it matters I'm using the newest version of ActivePerl on a Athlon and running ME.
-
-
It works for me!!
What does the rest of your code look like, maybe it's something to do with that
-
The below code works for me and displays the directory listing of d:\Perl in my browser window !
Code:
@files=`dir d:\\Perl\\`;
#display in browser
print"Content-Type: text/html\n\n";
foreach $line(@files){
$line =~ s/\W/ /gi;
print "$line<br>";
};
-
Uhm... @ is an array, not a scalar.
-
This is a feature of perl the result of the command is feeded into the array, and every time it hits a \n (newline) it creates a new index in the array!
same as if you did
Code:
$file = "myfile.txt";
open (MYFILE, "< $file") or die "can't open $file";
@lines = <MYFILE>;
close MYFILE;
This would read the whole file into the array and put each line in the next index in the array
-
Must be me. If I did that assignment, it would just create an array with a single element that was set equal to that string. Which is fine, as long as you know you put your string in an array.
I don't have Perl on this machine or I would play with it.
-
but the return of the command line that CaptainPinko did, and
my file reading example, both return multiline strings therfore each line will go as a new elemnet in the array
eg
Code:
######myfile.txt#####
one
two
three
four
###program#####
$file = "myfile.txt";
open (MYFILE, "< $file") or die "can't open $file";
@lines = <MYFILE>;
close MYFILE;
#print out the whole array quick method
print @lines;
print "\n\n";
#print out array line by line
foreach $entry (@lines){
#perform a serach and replace on each line and remove the letter o
$entry =~ s/o//gi;
print $entry;
}
######output#######
one
two
three
four
ne
tw
three
fur
:cool:
-
I've done "@lines = <MYFILE>;" before. My problem is.. I've not gotten a line in the script to execute at the shell without something like system().
-
you have to use ` and not '
-
@myFiles = `dir\\*.*`;
creates an array of strings from the output that is bascially the whole program.
`dir\\*.*` is not a string, its a commandline command b/c of the backticks `
works fine like above, but if i use a space i get "BAD COMMAND OR FILENAME" but w/ a s pace works fine when i go to the command line and type it in.
could this just be a bug?
-
It could be possibly try checking the active state website for info on ME
I can't replicate your error on my machine Win2000.
Iv'e tried running the script from the command line and from a browser and they both work fine.
Just check that all your paths are correct and the correct case, thats all i can suggest :confused: