The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Help with glob output script!
Discuss Help with glob output script! in the Perl Programming forum on Dev Shed. Help with glob output script! Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 11th, 2012, 02:03 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 32 m 8 sec
Reputation 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!
|

October 11th, 2012, 02:41 PM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
|
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.
|

October 11th, 2012, 02:47 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 32 m 8 sec
Reputation Power: 0
|
|
Quote: | 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;
|

October 11th, 2012, 05:18 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
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
|

October 11th, 2012, 05:20 PM
|
|
|
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:
Original
- Perl Code |
|
|
|
my $filename = "named_matched_${arg}.txt"; open (my $FH, ">", $filename) or die "cannot open $filename $!\n";
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|