Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > eval, "adding up" words to create a variable?

Reply
Thread Tools

eval, "adding up" words to create a variable?

 
 
Tomasz Chmielewski
Guest
Posts: n/a
 
      09-18-2008
I have a couple of variables, like:

my $thing_added = "test1";
my $otherthing_added = "test2";


I also have an array:

@array = ("thing", "otherthing", "no_such_variable");


Now, I would like to go through every element in the array, and check if
there is a corresponding "<array_element>_added" variable.


For example, for "thing", I would like to check if there is a
$thing_added variable. This should evaluate true, as I defined that
variable earlier.

Similarly, for "otherthing", I would like to check if there is a
$otherthing_added variable. This one should also evaluate true.

For "no_such_variable" element, a check for $no_such_variable_added
should evaluate false, as this variable was not defined.


I tried using "eval" to construct a variable out of yet another variable
value and a "_added" word, but haven't been successful.

Any hints?


--
Tomasz Chmielewski
http://wpkg.org
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      09-18-2008
Tomasz Chmielewski wrote:
> I have a couple of variables, like:
>
> my $thing_added = "test1";
> my $otherthing_added = "test2";
>
>
> I also have an array:
>
> @array = ("thing", "otherthing", "no_such_variable");
>
>
> Now, I would like to go through every element in the array, and check if
> there is a corresponding "<array_element>_added" variable.
>
>
> For example, for "thing", I would like to check if there is a
> $thing_added variable. This should evaluate true, as I defined that
> variable earlier.
>
> Similarly, for "otherthing", I would like to check if there is a
> $otherthing_added variable. This one should also evaluate true.
>
> For "no_such_variable" element, a check for $no_such_variable_added
> should evaluate false, as this variable was not defined.
>
>
> I tried using "eval" to construct a variable out of yet another variable
> value and a "_added" word, but haven't been successful.
>
> Any hints?


Every time you want to use variables as part of variable names, you're
better off using a hash.

Usually you save your life by using a hash as you usually get stoned to
death by asking these kinds of questions here.

Usually,

Josef

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      09-18-2008

Quoth Tomasz Chmielewski <>:
> I have a couple of variables, like:
>
> my $thing_added = "test1";
> my $otherthing_added = "test2";
>
>
> I also have an array:
>
> @array = ("thing", "otherthing", "no_such_variable");
>
>
> Now, I would like to go through every element in the array, and check if
> there is a corresponding "<array_element>_added" variable.


perldoc -q "variable name" explains why this is a bad idea (it's
actually impossible for 'my' variables, short of using deep magic like
PadWalker). What are you actually trying to do?

Ben

--
We do not stop playing because we grow old;
we grow old because we stop playing.

 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      09-18-2008

Tomasz Chmielewski wrote:
> I have a couple of variables, like:
>
> my $thing_added = "test1";
> my $otherthing_added = "test2";
>
>
> I also have an array:
>
> @array = ("thing", "otherthing", "no_such_variable");
>
>
> Now, I would like to go through every element in the array, and check if
> there is a corresponding "<array_element>_added" variable.
>
>
> For example, for "thing", I would like to check if there is a
> $thing_added variable. This should evaluate true, as I defined that
> variable earlier.
>
> Similarly, for "otherthing", I would like to check if there is a
> $otherthing_added variable. This one should also evaluate true.
>
> For "no_such_variable" element, a check for $no_such_variable_added
> should evaluate false, as this variable was not defined.
>
>
> I tried using "eval" to construct a variable out of yet another variable
> value and a "_added" word, but haven't been successful.
>
> Any hints?
>
>

$ perl -e '$x=foo; $foo=1; print defined($$x)?"Yes\n":"No\n"'
Yes

$ perl -e '$x=foo; $foo=0; print defined($$x)?"Yes\n":"No\n"'
Yes

$ perl -e '$x=foo; $bar=0; print defined($$x)?"Yes\n":"No\n"'
No

But IMHO you really really should be using hashes. Something not
entirely unlike:

my $thingies{thing_added} = "test1";
my $thingies{otherthing_added} = "test2";
...
for (@array) {
if (defined($thingies{$_})) {
...
}
}
(untested - caveat emptor)

I'm sure there's a FAQ that advises against the $$x kind of crazy
foolishness.

--
RGB
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      09-18-2008

Quoth RedGrittyBrick <>:
>
> $ perl -e '$x=foo; $foo=1; print defined($$x)?"Yes\n":"No\n"'
> Yes


~% perl -le'
use strict;
my $x = "foo";
my $foo = 1;
print defined($$x) ? "Yes" : "No";'
Can't use string ("foo") as a SCALAR ref while "strict refs" in use
at -e line 5

~% perl -le'
my $x = "foo";
my $foo = 1;
print defined($$x) ? "Yes" : "No";'
No

> I'm sure there's a FAQ that advises against the $$x kind of crazy
> foolishness.


perldoc -q "variable name"

There's also 'use strict', which forbids it entirely.

Ben

--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die.
 
Reply With Quote
 
Tomasz Chmielewski
Guest
Posts: n/a
 
      09-18-2008
Josef Moellers schrieb:
> Tomasz Chmielewski wrote:


(...)

>> Any hints?

>
> Every time you want to use variables as part of variable names, you're
> better off using a hash.
>
> Usually you save your life by using a hash as you usually get stoned to
> death by asking these kinds of questions here.


Ha ha, I take it as a good advice for the future then


--
Tomasz Chmielewski
http://wpkg.org

 
Reply With Quote
 
Tomasz Chmielewski
Guest
Posts: n/a
 
      09-18-2008
Ben Morrow schrieb:
> Quoth Tomasz Chmielewski <>:
>> I have a couple of variables, like:
>>
>> my $thing_added = "test1";
>> my $otherthing_added = "test2";
>>
>>
>> I also have an array:
>>
>> @array = ("thing", "otherthing", "no_such_variable");
>>
>>
>> Now, I would like to go through every element in the array, and check if
>> there is a corresponding "<array_element>_added" variable.

>
> perldoc -q "variable name" explains why this is a bad idea (it's
> actually impossible for 'my' variables, short of using deep magic like
> PadWalker). What are you actually trying to do?


I have a couple of such "ifs", and they differ only slightly:


if ($product_id_added == 0 && length $product_id) {
&add_params("product_id", $product_id, $lun, $driver);
}


if ($vendor_id_added == 0 && length $vendor_id) {
&add_params("vendor_id", $vendor_id, $lun, $driver);
}



I thought of replacing these all "ifs" which differ only slightly by
going through array elements, and replacing each part.

my @array = ("product_id", "vendor_id", ...)

But it looks like I have to rethink that approach.


--
Tomasz Chmielewski
http://wpkg.org
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-18-2008
Tomasz Chmielewski <> wrote:
>I have a couple of variables, like:
>my $thing_added = "test1";
>my $otherthing_added = "test2";
>
>I also have an array:
>@array = ("thing", "otherthing", "no_such_variable");
>
>Now, I would like to go through every element in the array, and check if
>there is a corresponding "<array_element>_added" variable.

[...]
>Any hints?


Short answer: don't do that, it is A Bad Idea (TM).
Longer answer: you are looking for symbolic references. Please see the
history of this NG as well as the FAQ ("variable as a variable name")
why that is not a good idea.
Yet longer answer: Use a hash, dude, that's what they are for.

my %added;
$added{'thing'} = 'test1';
$added{'otherthing'} = 'test2';
.....
if (exists($added{'thing'})) {
print "Someone has added 'thing' to \%added";
}

jue
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      09-18-2008
Tomasz Chmielewski <> writes:

> I have a couple of such "ifs", and they differ only slightly:
>
>
> if ($product_id_added == 0 && length $product_id) {
> &add_params("product_id", $product_id, $lun, $driver);
> }
>
>
> if ($vendor_id_added == 0 && length $vendor_id) {
> &add_params("vendor_id", $vendor_id, $lun, $driver);
> }
>
>
>
> I thought of replacing these all "ifs" which differ only slightly by
> going through array elements, and replacing each part.
>
> my @array = ("product_id", "vendor_id", ...)
>
> But it looks like I have to rethink that approach.


You should. If you choose the right datastructure, you can probably
reduce the amount of code a lot more than if you "just" use symbolic
references here and there.

Also, it looks like you're using 2 flags to mark if something was
added. You won't need to if you're using a hash:

my %added;

# add a vendor id
$added{vendor} = 2;

# don't do anything with products

for my $key (qw(vendor products foo)) {
next unless exists $added{$key};
add_params("${key}_id",$added{$key},...)
}

also: IMHO it's bad style to use the &foo() construct to call
functions. foo() is a little shorted and has less potential problems.


--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
Tomasz Chmielewski
Guest
Posts: n/a
 
      09-18-2008
Joost Diepenmaat schrieb:
> Tomasz Chmielewski <> writes:
>
>> I have a couple of such "ifs", and they differ only slightly:
>>
>>
>> if ($product_id_added == 0 && length $product_id) {
>> &add_params("product_id", $product_id, $lun, $driver);
>> }
>>
>>
>> if ($vendor_id_added == 0 && length $vendor_id) {
>> &add_params("vendor_id", $vendor_id, $lun, $driver);
>> }
>>
>>
>>
>> I thought of replacing these all "ifs" which differ only slightly by
>> going through array elements, and replacing each part.
>>
>> my @array = ("product_id", "vendor_id", ...)
>>
>> But it looks like I have to rethink that approach.

>
> You should. If you choose the right datastructure, you can probably
> reduce the amount of code a lot more than if you "just" use symbolic
> references here and there.
>
> Also, it looks like you're using 2 flags to mark if something was
> added. You won't need to if you're using a hash:
>
> my %added;
>
> # add a vendor id
> $added{vendor} = 2;
>
> # don't do anything with products
>
> for my $key (qw(vendor products foo)) {
> next unless exists $added{$key};
> add_params("${key}_id",$added{$key},...)
> }


Thanks for the tip.


> also: IMHO it's bad style to use the &foo() construct to call
> functions. foo() is a little shorted and has less potential problems.


Besides being "bad style", what potential problems can it make? (code
readability? something else?)


--
Tomasz Chmielewski
http://wpkg.org
 
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
Re: Words and non-words, according to Microsoft et al Steve B NZ Computing 11 03-21-2008 11:52 PM
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
Words Words utab C++ 6 02-16-2006 07:00 PM
Non-noise words are incorrectly recognised as noise words. Peter Strøiman ASP .Net 1 08-23-2005 01:26 PM
Re: A little bit of help regarding my linked list program required. - "words.c" - "words.c" Richard Heathfield C Programming 7 10-05-2003 02:38 PM



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