LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 03-09-2003, 03:17 PM   #1
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Rep: Reputation: 30
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

????
 
Old 03-09-2003, 04:52 PM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Well, you need to use the @ARGV array.

Alex
 
Old 03-09-2003, 04:56 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
probably want Getopts in there too if it gets more complex.
 
Old 03-09-2003, 05:13 PM   #4
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Original Poster
Rep: Reputation: 30
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[])?
 
Old 03-09-2003, 05:15 PM   #5
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
It's just an already-existing global variable.

Alex
 
Old 03-10-2003, 04:47 AM   #6
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Original Poster
Rep: Reputation: 30
ok, ok... now really...
what would this code look like? :-)
 
Old 03-10-2003, 04:57 AM   #7
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
@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
 
Old 03-10-2003, 05:02 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
sounds a lot like an RTFM to me, JStew!
 
Old 03-10-2003, 06:24 AM   #9
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Original Poster
Rep: Reputation: 30
yeah, it is acid-- i really tried to disguise it as best i could but, you know ignorance... hard to conceal it at times :-)
 
Old 03-10-2003, 06:26 AM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
yeah, that and old age are a bad combination i guess.....
 
Old 03-10-2003, 06:56 AM   #11
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Original Poster
Rep: Reputation: 30
<grumble> well, thanks a lot acid ... you killed the thread! </grumble>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with passing arguments in Perl bahadur Programming 1 05-30-2005 01:47 AM
perl arguments zaken Programming 1 02-27-2005 11:19 AM
command arguments in perl djgerbavore Programming 1 12-30-2004 08:06 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Function accept() and its arguments. krajzega Programming 1 01-20-2004 01:26 AM


All times are GMT -5. The time now is 06:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration