|
-
Dec 4th, 2001, 06:38 PM
#1
Thread Starter
Hyperactive Member
PERL: Simple Problem
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.
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Dec 5th, 2001, 09:41 AM
#2
Hyperactive Member
Last edited by progressive; Dec 5th, 2001 at 09:45 AM.
-
Dec 5th, 2001, 09:50 AM
#3
Hyperactive Member
It works for me!!
What does the rest of your code look like, maybe it's something to do with that
-
Dec 5th, 2001, 10:03 AM
#4
Hyperactive Member
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>";
};
-
Dec 5th, 2001, 10:04 AM
#5
Frenzied Member
Uhm... @ is an array, not a scalar.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Dec 5th, 2001, 10:09 AM
#6
Hyperactive Member
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
-
Dec 5th, 2001, 10:13 AM
#7
Frenzied Member
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.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Dec 5th, 2001, 10:37 AM
#8
Hyperactive Member
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
-
Dec 5th, 2001, 10:54 AM
#9
Frenzied Member
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().
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Dec 5th, 2001, 10:55 AM
#10
Hyperactive Member
you have to use ` and not '
-
Dec 5th, 2001, 03:29 PM
#11
Thread Starter
Hyperactive Member
@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?
Last edited by CaptainPinko; Dec 5th, 2001 at 03:33 PM.
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Dec 6th, 2001, 05:12 AM
#12
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|