dynamic scalar-naming problem

H

Hiro San Feng

key="hi" to the perl community!

what is wrong with this code?


for html reasons is need 3 version of a field-value,
1. the pure value as in the db
2. a " " to "+" version for the URL
3. a complete built link

all data-field scalars are name field-n
now I iterate thtough them... and get syntax errors!


for(my $i = 1; $i <= 55; $i++)
# add a leading zero to first 9 fields
# so field-9 becomes field-09
if($i < 10){ $i = "0" . $i; }
# do the conversion
{"field-" . $i . "_A"} = {"field" . $i};
{"field-" . $i}=~ tr/ /+/;
{"field-" . $i . "_B"} = "<A HREF='foo.com?key=" .
"field-" . $i .
"'>" . {"field-" . $i} . "</A>";
}

thanks for any hints!
as it seems the perl syntax is not so logic as it seems... at least to
me.
 
G

gnari

Hiro San Feng said:
key="hi" to the perl community!

what is wrong with this code?

for(my $i = 1; $i <= 55; $i++)
# add a leading zero to first 9 fields
# so field-9 becomes field-09
if($i < 10){ $i = "0" . $i; }

take a look at sprintf
# do the conversion
{"field-" . $i . "_A"} = {"field" . $i};
[snipped rest of ths horror]

what you are trying to do is ${"field$i"}, but
you should use a hash or an array for this kind of data.

do not keep you fields in scalars $field01, $field02 ... $field55
but rather if the array @fields


gnari
 
G

Gunnar Hjalmarsson

Hiro said:
for html reasons is need 3 version of a field-value,
1. the pure value as in the db
2. a " " to "+" version for the URL
3. a complete built link

all data-field scalars are name field-n
now I iterate thtough them...

No you don't.
and get syntax errors!

Yes, that sounds more likely.
# do the conversion
{"field-" . $i . "_A"} = {"field" . $i};
{"field-" . $i}=~ tr/ /+/;
{"field-" . $i . "_B"} = "<A HREF='foo.com?key=" .
"field-" . $i .
"'>" . {"field-" . $i} . "</A>";
}

thanks for any hints!

Have you possibly considered to assign the various versions to Perl
variables?

http://learn.perl.org/
 
A

Andy Baxter

At earth time Sun, 25 Jan 2004 15:51:11 +0000, the following transmission
was received from the entity known as Hiro San Feng:
key="hi" to the perl community!

what is wrong with this code?


for html reasons is need 3 version of a field-value,
1. the pure value as in the db
2. a " " to "+" version for the URL
3. a complete built link

all data-field scalars are name field-n
now I iterate thtough them... and get syntax errors!


for(my $i = 1; $i <= 55; $i++)
# add a leading zero to first 9 fields
# so field-9 becomes field-09
if($i < 10){ $i = "0" . $i; }
# do the conversion
{"field-" . $i . "_A"} = {"field" . $i};
{"field-" . $i}=~ tr/ /+/;
{"field-" . $i . "_B"} = "<A HREF='foo.com?key=" .
"field-" . $i .
"'>" . {"field-" . $i} . "</A>";
}

thanks for any hints!
as it seems the perl syntax is not so logic as it seems... at least to
me.

why not just keep all the raw values in a hash or array, then convert them
to the other two forms as needed?

e.g.

my @fields=('field1-value','field2-value','field3value' etc. );

for (my $i=0; $i<scalar (@fields); $i++) {
my $field=$fields;
my $htmlfield="<A HREF='foo.com?key=field-" . $i .
"'>" . $field . "</A>";
my $plusfield=$field;
$plusfield =~ tr/ /+/;
# do something with $field, $htmlfield, and $plusfield
# ...
} # and go on to the next one.
 
M

Mark Jason Dominus

for(my $i = 1; $i <= 55; $i++)
# add a leading zero to first 9 fields
# so field-9 becomes field-09
if($i < 10){ $i = "0" . $i; }
# do the conversion
{"field-" . $i . "_A"} = {"field" . $i};
{"field-" . $i}=~ tr/ /+/;
{"field-" . $i . "_B"} = "<A HREF='foo.com?key=" .
"field-" . $i .
"'>" . {"field-" . $i} . "</A>";
}

Sir,

I suggest the following:

for my $i ( "01" .. "55" ) {
$field[$i]{A} = ${"field$i"};
$field[$i]{C} = ${"field$i"};
$field[$i]{C} =~ tr/ /+/;
as it seems the perl syntax is not so logic as it seems...

You may be the first person ever to suggest that the Perl syntax seems logical.

My best regards, and a happy new year.
 
H

Hiro San Feng

key="hi" to the perl community!
<snip>

"@?!;!!" doesn't express how blind I was,
I should switch off the box for today...

now it works:

# test
$field09 = "Hello World";

for(my $i = 1; $i <= 55; $i++)
{
if($i < 10){ $i = sprintf("%02d", $i); }
#
${"field" . "$i". "_A"} = ${"field$i"};
${"field$i"}=~ tr/ /+/;
${"field" . "$i" . "_B"} = "<A HREF='foo.com?field$i=" . ${"field" .
"$i"} . "'>" . ${"field" . $i . "_A"} . "</A>";
}

print "$i: $field09; $field09_A; $field09_B";
 
G

gnari

Hiro San Feng said:
# test
$field09 = "Hello World";

for(my $i = 1; $i <= 55; $i++)
{
if($i < 10){ $i = sprintf("%02d", $i); }

the point of the sprintf is you dont really need the test

also, I get nervous when I see assignements to the index
of this type of loop. I would either use :
for my $i (1..55) {...
or
for(my $x = 1; $x <= 55; $x++) {
my $i=sprintf("%02d", $x);

#
${"field" . "$i". "_A"} = ${"field$i"};
${"field$i"}=~ tr/ /+/;
${"field" . "$i" . "_B"} = "<A HREF='foo.com?field$i=" . ${"field" .
"$i"} . "'>" . ${"field" . $i . "_A"} . "</A>";
}

print "$i: $field09; $field09_A; $field09_B";

here I would again suggest using some different datastructures.
either 3 arrays, or an array of hashs

gnari
 
U

Uri Guttman

HSF> "@?!;!!" doesn't express how blind I was,
HSF> I should switch off the box for today...

you are still blind. see below

HSF> now it works:

HSF> #
HSF> ${"field" . "$i". "_A"} = ${"field$i"};
HSF> ${"field$i"}=~ tr/ /+/;
HSF> ${"field" . "$i" . "_B"} = "<A HREF='foo.com?field$i=" . ${"field" .
HSF> "$i"} . "'>" . ${"field" . $i . "_A"} . "</A>";
HSF> }

those are symbolic references and should not be used for simple data
structures. the symbol table is just a special hash tree and gives you
no benefits over a plain hash structure. but using the symbol table like
that can be very dangerous and make for hard to find bugs.

another benefit of plain hashes is you can pass them around (by ref or
by copy) and make many of them. hard to do that with the single global
symbol table.

read perlreftut, perlref, perldsc and perllol to learn how to do proper
perl data structures. others mentioned this in this thread but you have
gone on the symref path of evil code.

rule: use symrefs only when you must mung the symbol table. otherwise a
plain hash is safer, more useful and strict clean.

uri
 
M

Michele Dondi

now it works: ^^^^^^^^^^^^

# test
$field09 = "Hello World";

for(my $i = 1; $i <= 55; $i++)
{
if($i < 10){ $i = sprintf("%02d", $i); }
#
${"field" . "$i". "_A"} = ${"field$i"};
${"field$i"}=~ tr/ /+/;
${"field" . "$i" . "_B"} = "<A HREF='foo.com?field$i=" . ${"field" .
"$i"} . "'>" . ${"field" . $i . "_A"} . "</A>";
}

Beware though: you've followed only some of the advices you've been
given! More explicitly, you should know that generally an article like
yours tends to raise meta-discussions about what one should tell to a
newbie about such questions.

The key point is the use of symbolic references. They all agree that
it is potentially dangerous. Now, there are two main "parties" in this
respect: some think that it is better to tell about them anyway,
others that you should not mention them at all.

A good point about the first policy is that if the newbie is not told
about symbolic references and uses, say, a hash instead, when he later
will discover them he'll think, *erroneusly*, he's found a better way
to do it.

Now trust me: there are good reasons why the "best way to do it" is by
avoiding the use of symrefs. In your code snippet above this wouldn't
make much difference, but if you get used to them you will be bitten
first or later.

Other posters will be more than happy to shed some more light on this
topic, if you're interested. Also, googling may help...


Michele
 
M

Michele Dondi

[snip]

<OT>
FWIW, *welcome back here*! Even if maybe this is not appropriate since
IIRC you weren't here when *I* started to read the ng regularly, but
kinda felt like saying so...
</OT>


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top