Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-09-2003, 03:17 PM
|
#1
|
|
Member
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229
Rep:
|
Perl: Getting a script to accept arguments
Here is what I wrote so far:
#! /usr/bin/perl -w
# match.pl
system("clear");
print "Please enter a file to be searched: ";
$file_searched = <STDIN>; chomp $file_searched;
open (FILE1, $file_searched) || die "File not found.\n";
@line = <FILE1>;
close FILE1;
print "What text pattern do you want to search for? ";
$pattern = <STDIN>; chomp $pattern;
$line_number = 1;
foreach $line (@line) {
if($line =~ m/$pattern/g) {
print "Found another \"$pattern\" at Line Number: $line_number.\n";
}
$line_number++;
}
exit 0;
END OF CODE
What can i do to add to this that will accept an option like this:
./match.pl filename search_string
????
|
|
|
|
03-09-2003, 04:52 PM
|
#2
|
|
Member
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590
Rep:
|
Well, you need to use the @ARGV array.
Alex
|
|
|
|
03-09-2003, 04:56 PM
|
#3
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,676
|
probably want Getopts in there too if it gets more complex.
|
|
|
|
03-09-2003, 05:13 PM
|
#4
|
|
Member
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229
Original Poster
Rep:
|
Quote:
|
Well, you need to use the @ARGV array.
|
How would I implement that into a Perl script since it doesnt have an int main(int argc, char *argv[])?
|
|
|
|
03-09-2003, 05:15 PM
|
#5
|
|
Member
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590
Rep:
|
It's just an already-existing global variable.
Alex
|
|
|
|
03-10-2003, 04:47 AM
|
#6
|
|
Member
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229
Original Poster
Rep:
|
ok, ok... now really...
what would this code look like? :-)
|
|
|
|
03-10-2003, 04:57 AM
|
#7
|
|
Member
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590
Rep:
|
@ARGV is just an array of the arguments passed to the program (which I can't be bothered to write for you just now; someone else might). There's a lot of freely available documentation on these kinds of Perl basics, not least the comprehensive man page documentation that comes with Perl.
Alex
|
|
|
|
03-10-2003, 05:02 AM
|
#8
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,676
|
sounds a lot like an RTFM to me, JStew! 
|
|
|
|
03-10-2003, 06:24 AM
|
#9
|
|
Member
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229
Original Poster
Rep:
|
yeah, it is acid-- i really tried to disguise it as best i could but, you know ignorance... hard to conceal it at times :-)
|
|
|
|
03-10-2003, 06:26 AM
|
#10
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,676
|
yeah, that and old age are a bad combination i guess.....
|
|
|
|
03-10-2003, 06:56 AM
|
#11
|
|
Member
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229
Original Poster
Rep:
|
<grumble> well, thanks a lot acid ... you killed the thread! </grumble>
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:52 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|