Find the answer to your Linux question:
Results 1 to 10 of 10
This perl script is not out putting correctly. It takes the $dir name from a file in a while loop and is supposed to output the $dir name, then it ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2005
    Posts
    8

    Perl scripting help please - Output not outputting correctly


    This perl script is not out putting correctly.
    It takes the $dir name from a file in a while loop and is supposed to output the $dir name, then it does a count of the files inside of the directory and is supposed to put it after the directory like

    <Directory name> The file count is : <number>

    instead its doing ....

    <number>
    <Directory name> The file count is : (puts nothing)
    <number>
    <Directory name> The file count is : (puts nothing)

    The number comes first, goes to a new line, and then places the directory name. Obviously I want the number to come after the directory name, not above and before the directory name =D

    Here is what I have ....

    $DIR_FILE = "DirectoryList.txt";
    open (ASCII_DIR, "< $DIR_FILE") || die("Cannot open the data file for INPUT: $!");
    LINE: while ($dir = <ASCII_DIR>) {

    chomp($dir);
    print "$dir file count is: ";
    system("ls $dir | wc -l");
    print "\n";



    Any help would be great!
    Thanks in advance

  2. #2
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Code:
    $DIR_FILE = "DirectoryList.txt";
    open &#40;ASCII_DIR, "< $DIR_FILE"&#41; || die&#40;"Cannot open the data file for INPUT&#58; $!"&#41;;
    LINE&#58; while &#40;$dir = <ASCII_DIR>&#41; &#123;
       chomp&#40;$dir&#41;;
       $count = `ls $dir | wc -l`;
       print "$dir file count is&#58; $count\n";
       print "\n"; 
    &#125;
    This code should do what you intended; Personally, I try to avoid using system when I want output from a program; I use the backticks or pipes for that. As far as why your output was so bizzare, I can't offer any insight. =(

  3. #3
    Just Joined!
    Join Date
    Sep 2005
    Posts
    8
    Could I/should I try and put

    print "$dir file count is: ";
    system("ls $dir | wc -l");


    Into one line and see how that works?
    I am not a perl guru here, I had help writing this script above, so just wondering how I would combine those two into 1 print line?

    But perhaps if I had them combined into 1 line, then it will have to come after the directory :P

  4. #4
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217

    Re: Perl scripting help please - Output not outputting correctly

    Quote Originally Posted by xponet
    print "$dir file count is: ";
    system("ls $dir | wc -l");
    print "\n";
    most likely it has to do with buffering ... change your print() to warn() (warn prints to stderr which is not buffered) and I bet you see something more like what you expected

    however, I agree that you should probably be using backticks instead.
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  5. #5
    Just Joined!
    Join Date
    Sep 2005
    Posts
    8
    Thanks for the "warn" ... it helped with the output, but at the same time made it even messier.

    It now displays it after what I want it to, but now it also displays some extra jibberish like this ....

    </directory/sub> file count is: at Test.pl line 31, <ASCII_DIR> line 41.
    0


    </directory/sub> file count is: at Test.pl line 31, <ASCII_DIR> line 42.
    315

    so it is displaying an extra "at Test.pl line 31, <ASCII_DIR> line 42." now
    Kinda odd

  6. #6
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    oops ... yeah I forgot about that ... warn will print the line number, etc ... if you don't include a newline ("\n")

    I didn't mean to suggest that you should use warn ... you shouldn't for what you are trying to do ... I was only trying to show that it was due to buffereing that you were seeing the count before the description.
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  7. #7
    Just Joined!
    Join Date
    Sep 2005
    Posts
    8
    Ahh yeah withthe \n it doesnt print that all out and prints the nubmers at the end of the line (well on the next line , but afterwards) ... Cool thanks.

    Now to figure out how to make it worth by putting it at the end of the line.

    Instead of using the system I should be able to use a perl command or perl call to do what my "system" function is donig correct?

    any help on those commands would be great =)

  8. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    You can still use "system" for running actual system commands, for example:

    Code:
    # Blah blah blah
    print "3. Compile\n";
    print "Choice&#58; ";
    $choice = <STDIN>;
    
    system "g++ thefile.cpp" if $choice == 3;
    
    #Blah blah blah
    However, if you want to work the command directly into your program, and use it for output / modifying, backticks are the way to go:

    Code:
    chomp&#40;@dirs = `ls -1`&#41;;
    
    print "$_\n" for &#40;@dirs&#41;;

    Also, for that whole print thing, output to a terminal is not buffered, while output to anything else is. To turn off buffering, set the value of $| to 1:

    Code:
    $| = 1

    Okie dokie?

  9. #9
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    One possible option:

    Code:
    #!/usr/bin/perl
    
    $DIR_FILE = "DirectoryList.txt";
    open&#40;ASCII_DIR, "<$DIR_FILE"&#41; || die&#40;"Cannot open the data file for INPUT&#58; $!"&#41;;
    
    while &#40;$dir = <ASCII_DIR>&#41; &#123;
       chomp&#40;$dir&#41;;
       printf&#40;"%s file count is&#58; %d\n", $dir, scalar&#40;@count = `ls $dir`&#41;&#41;;
    &#125;
    close&#40;ASCII_DIR&#41;;

  10. #10
    Just Joined!
    Join Date
    Sep 2005
    Posts
    8
    Thanks for all the help ...

    the $| = 1 actually did the trick!

    I am going to try some of the other suggestions as well

    again,
    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •