Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using command line argument as variable name

Reply
Thread Tools

Using command line argument as variable name

 
 
Al
Guest
Posts: n/a
 
      06-23-2006
How do I escape (or whatever) a command line argument so that I can use
it's content as a variable name in my program? E.g.:

myprogram myvariable

if ($myvariable =~ ....

Thanks much in advance,
Al

 
Reply With Quote
 
 
 
 
Nick of course
Guest
Posts: n/a
 
      06-23-2006

Al wrote:
> How do I escape (or whatever) a command line argument so that I can use
> it's content as a variable name in my program? E.g.:
>
> myprogram myvariable
>
> if ($myvariable =~ ....
>
> Thanks much in advance,
> Al

Sense not question make

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      06-23-2006
Al wrote:
> How do I escape (or whatever) a command line argument so that I can
> use it's content as a variable name in my program? E.g.:
>
> myprogram myvariable
>
> if ($myvariable =~ ....


The keyword you are looking for is symbolic references.
Please see the FAQ ("How can I use a variable as a variable name?") and
numerous earlier postings for details about why this is a Bad Idea (TM) and
what to do instead.

jue


 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      06-23-2006

Al wrote:
> How do I escape (or whatever) a command line argument so that I can use
> it's content as a variable name in my program? E.g.:
>
> myprogram myvariable
>
> if ($myvariable =~ ....


If I'm understanding your intent...

my_script.pl param1 param2

#!/usr/bin/perl

use strict; use warnings;

my ( $arg1, $arg2 ) = @ARGV;

if ( $arg1 =~ m/para/ ) {
print "yay!\n";
}
else {
print "boo!\n";
}

 
Reply With Quote
 
darksaga
Guest
Posts: n/a
 
      06-23-2006
use Getopt::Long;

my $msg;
my $msg2;
GetOptions(
'-FLAGNAME=s' => \$msg,
'-FLAGNAME2=s' => \$msg2
);
print "$msg\n$msg2\n"

call your script as follows:
perl myScript.pl -FLAGNAME hello -FLAGNAME2 world

greetz darksaga

 
Reply With Quote
 
Al
Guest
Posts: n/a
 
      06-23-2006
Nick of course wrote:
> Al wrote:
> > How do I escape (or whatever) a command line argument so that I can use
> > it's content as a variable name in my program? E.g.:
> >
> > myprogram myvariable
> >
> > if ($myvariable =~ ....
> >
> > Thanks much in advance,
> > Al

> Sense not question make


Okay - let's say I have a program called: myprogram, and a command line
argument of: myvariable. I run the program like this:

myprogram myvariable

Within my program, there is an existing variable called: $myvariable

How can I address this variable in terms of the command line argument -
i.e., how do I find the value of $myvariable by referencing what was
passed on the command line? For example:

if ([command line argument substitution designating $myvariable] =~
'hello' )...

Sense make more?

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-23-2006
Al wrote:
> Okay - let's say I have a program called: myprogram, and a command
> line argument of: myvariable. I run the program like this:
>
> myprogram myvariable
>
> Within my program, there is an existing variable called: $myvariable
>
> How can I address this variable in terms of the command line argument
> - i.e., how do I find the value of $myvariable by referencing what was
> passed on the command line? For example:
>
> if ([command line argument substitution designating $myvariable] =~
> 'hello' )...
>
> Sense make more?


Are you actually reading what people are writing? Again:

YOU ARE LOOKING FOR SYMBOLIC REFERENCES. SEE THE FAQ AND DEJANEWS FOR
DETAILS ABOUT WHY THEY ARE EVIL AND WHAT TO USE INSTEAD.

jue


 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      06-23-2006
Thus spoke Al (on 2006-06-23 16:24):

> Within my program, there is an existing variable called: $myvariable
>
> How can I address this variable in terms of the command line argument -
> i.e., how do I find the value of $myvariable by referencing what was
> passed on the command line? For example:
>
> if ([command line argument substitution designating $myvariable] =~
> 'hello' )...


Names are hash entries in the perl guts,
so use your own hash for your names, like:

my %NAMES;
my ($varname) = shift; # <== will be 'myvariable' etc.

$NAMES{ $varname } = 1e-4;

print "name of variable was: ",
grep { /$varname/ } keys %NAMES;

# above is basically the same as :
# print "name of variable was: ", $varname;

print "\nactual value of it is: ", $NAMES{ $varname }, "\n";


Don't use Perls Name-Hash directly, as most others
here have already said.

Regards

Mirco


 
Reply With Quote
 
Al
Guest
Posts: n/a
 
      06-23-2006
Thanks very much for the feedback everybody!
- Al

Jürgen Exner wrote:
> Al wrote:
> > How do I escape (or whatever) a command line argument so that I can
> > use it's content as a variable name in my program? E.g.:
> >
> > myprogram myvariable
> >
> > if ($myvariable =~ ....

>
> The keyword you are looking for is symbolic references.
> Please see the FAQ ("How can I use a variable as a variable name?") and
> numerous earlier postings for details about why this is a Bad Idea (TM) and
> what to do instead.
>
> jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57