Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
No such thing as a small change
 
PerlMonks  

Taking Keyboard Input from the command line

by OxYgEn (Acolyte)
on Feb 26, 2004 at 00:57 UTC ( #331882=perlquestion: print w/ replies, xml ) Need Help??
OxYgEn has asked for the wisdom of the Perl Monks concerning the following question:

I've seen it done in some scripts I've downloaded, but after carefully searching them, I don't see how they've done it. Basically all I'm trying to do is add or remove stuff from an array, so I could run something like this:

perl happyscript.pl add something

and "something" would be added to a predefined array. Alternatively, to remove "something" from an array, I would type

perl happyscript.pl del something

Thanks for your patience on my incredibly stupid questions.

Comment on Taking Keyboard Input from the command line
Re: Taking Keyboard Input from the command line
by Abigail-II (Bishop) on Feb 26, 2004 at 01:03 UTC
    Something like:
    $ARGV [0] eq "add" ? do {push @predefined => $ARGV [1]} : $ARGV [0] eq "del" ? do {@predefined = grep {$_ ne $ARGV [1]} @predefi +ned} : die "Unknown command"
    perhaps?

    Abigail

Re: Taking Keyboard Input from the command line
by Limbic~Region (Chancellor) on Feb 26, 2004 at 01:04 UTC
    OxYgEn,
    I am not exactly sure this is what you want, but this should get you started. You might also want to take a look at Getopt::Std
    #!/usr/bin/perl use strict; use warnings; my @array = qw(foo bar blah asdf); if ( defined $ARGV[0] && defined $ARGV[1] ) { push @array , $ARGV[1] if $ARGV[0] eq 'add'; @array = grep {$_ !~ /^$ARGV[1]$/} @array if $ARGV[0] eq 'del'; } print "$_\n" for @array;
    Cheers - L~R

    Update: Corrected syntax error

Re: Taking Keyboard Input from the command line
by OxYgEn (Acolyte) on Feb 26, 2004 at 23:55 UTC
    Limbic~Region, I believe that is what I am looking for, but when I use your code this is what I get:

    Array found where operator expected at simple.pl line 8, near "/^$ARGV1$/ "
    (Missing operator before ?)
    syntax error at simple.pl line 8, near "/^$ARGV1$/ @array "
    Execution of simple.pl aborted due to compilation errors.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://331882]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-04-21 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls