Results 1 to 12 of 12

Thread: PERL: Simple Problem

  1. #1

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Angry 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

  2. #2
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    try...er never mind
    Last edited by progressive; Dec 5th, 2001 at 09:45 AM.

  3. #3
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    It works for me!!

    What does the rest of your code look like, maybe it's something to do with that

  4. #4
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    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>";
    	
    };

  5. #5
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    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.

  6. #6
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    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

  7. #7
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    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.

  8. #8
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    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

  9. #9
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    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.

  10. #10
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    you have to use ` and not '

  11. #11

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Unhappy

    @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

  12. #12
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    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
  •  



Click Here to Expand Forum to Full Width