#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Help with glob output script!


    1 a) Using glob_length.pl, cmdline_args.pl, and file_output.pl
    as a guide, write a script named count_matched.pl
    Your script should use one command line argument that specifies a
    filename extension. Use the glob function to get an array of files
    having the specified filename extension and print the number of matching
    files found. Add comments to explain the code you write. You will lose
    points for inadequate comments. Output from an example run might look like this:

    [service1][~/ecol553_week7]> perl count_matched.pl “bln”
    There are 3 files with a .bln extension

    1 b) Instead of writing output to the screen,
    open an output file named matched_xxx.txt, where xxx is the pattern
    specified as an argument. An example run might look like this:

    [service1][~/ ecol553_week7]> perl count_matched.pl “bln”
    [service1][~/ ecol553_week7]> cat matched_bln.txt
    There are 3 files with a .bln extension

    THIS IS WHAT I HAVE SO FAR:

    #!/usr/bin/perl
    use warnings;
    use strict;

    #initializes my variable and array
    my $ext;
    my @ext_filenames;

    #denotes the files that I am passing as arguments
    $ext = "txt";
    @ext_filenames = glob("*.$ext");

    #counts the number of arguments in your array
    my $count = scalar (@ext_filenames);

    #prints your results
    print "There are $count files wiht a .txt extension.";

    Please help!
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    You haven't said what the problem is! If you tell us where you're stuck, or what problems you're having with the code you've written, we might be better able to help.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by ishnid
    You haven't said what the problem is! If you tell us where you're stuck, or what problems you're having with the code you've written, we might be better able to help.
    Hi I am having problem with opening an output file named matched_xxx.txt, where xxx is the pattern specified as an argument.

    How do I go about doing it?

    I was thinking that is could be something like:

    my $file = "There are $count files with a .bln extension.";

    open(BFIL, $file) or die "Cannot open $file\n";
    open(NFIL, ">matched_$ext.txt") or die "Cannot write matched_$ext\n";

    close NFIL;
    close BFIL;
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Look at following code and see if it helps:
    Code:
    my $ext    =  ".bin";
    my $count  =  1;
    my $file   =  "There are $count files with a $ext extension.txt";
    
    open(BFIL, ">$file") or die "Cannot open $file\n";
    open(NFIL, ">matched_$ext.txt") or die "Cannot write matched_$ext\n";
    
    close NFIL;
    close BFIL;
    
    -- Files created:
    There are 1 files with a .bin extension.txt
    matched_.bin.txt

    http://www.tutorialspoint.com/perl/perl_open.htm
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    837
    Rep Power
    496
    If you want to get the argument passed to the script, and then open a file using this argument in the name, do something like this:

    Perl Code:
    my $arg = shift;
    chomp $arg;
    my $filename = "named_matched_${arg}.txt";
    open (my $FH,  ">",  $filename) or die "cannot open $filename $!\n";

IMN logo majestic logo threadwatch logo seochat tools logo