Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 11th, 2012, 02:03 PM
noobie2 noobie2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 noobie2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old October 11th, 2012, 02:41 PM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 49 m 41 sec
Reputation Power: 3885
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.

Reply With Quote
  #3  
Old October 11th, 2012, 02:47 PM
noobie2 noobie2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 noobie2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

Reply With Quote
  #4  
Old October 11th, 2012, 05:18 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 203 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 16 h 10 sec
Reputation Power: 41
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

Reply With Quote
  #5  
Old October 11th, 2012, 05:20 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 544 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 5 Days 2 h 25 m 5 sec
Reputation Power: 406
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
  1. my $arg = shift;
  2. chomp $arg;
  3. my $filename = "named_matched_${arg}.txt";
  4. open (my $FH">"$filename) or die "cannot open $filename $!\n";

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Help with glob output script!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap