[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-WebCGI] [MacPerl] Referencing a Hash element...



At 8:40 PM 12/27/00, Richard Smith wrote:
>Hello,
>
>Below is a snippet of code from a CGI script I am building. I have been
>trying to work out how to create a reference to the hash elements to save
>constant repetition of the full Hash element name (i.e.
>$Form{"description$tagIndex"} ), but I am having no luck. I have read and
>re-read the relevant section in 'Programming Perl', but i am none the wiser
>as to the necessary syntax. Any pointers would be much appreciated. Please
>feel free to rubbish my bloated code too.

[big snip of code]

Richard -- Please clarify: When you say you want to "save constant 
repetition of the full Hash element name (i.e. 
$Form{"description$tagIndex"} )", do you mean to save typing time, to 
conserve bytes/electrons, to avoid excessive accesses of a variable, 
to make your code easy for someone else to understand, to make your 
code "elegant", or what?

Except for "elegance" -- a suspect goal IMO (poetry reveals, elegance 
conceals; but that's another topic... ;-) -- these various and 
sometimes conflicting goals might all benefit from some higher-level 
structural changes in how you're processing your form input. I'm not 
sure how much you'd gain from using references, but maybe I'm not 
seeing your big picture here. It would help to have an outline of the 
whole process.

What I gather is that you have some base hash keys (linktext, 
description, location, order, ...), and you're also iterating through 
some series of $tagIndex (although your code snippet doesn't show 
what terminates that series), so you have a bunch of hash elements 
like $Form{description1}, $Form{description2}, ..., $Form{linktext1}, 
$Form{linktext2}, ...

And you're doing several things to some or all of those hash elements:
-remove line-endings
-trim whitespace
-replace non-alphas with '*'
-terminate (description elements) with '.'
-populating an error message as needed

This could be handled efficiently with some lists and loops, but I'd 
rather not make them up without being sure what your process is.

OTOH, if I'm just going off on my own jag, here's what might be an 
answer to your literal question about employing references:

#####

#!perl -w

my %Form;
my $tagIndex = 1;

$Form{"description$tagIndex"} = ' A big %!@#$& dog with 0-0 eyes ... ';

# make a reference with preceding '\'
my $Fd = \$Form{"description$tagIndex"};

# process ref (de-referenced with preceding '$')
# trim
$$Fd =~ s/^\s*(.*?)\s*$/$1/;

# full stop at end (Yanks call it a period)
$$Fd =~ s/\s*\.*$/./;        # Do this *before* next step

# censor
$$Fd =~ s/[^a-zA-Z .]+/*/g;  # Don't need to escape the *
                              #  in the replacement side of s///
                              # Also: add '.' to OK chars

# Lo, the original value has been processed
print $Form{"description$tagIndex"};

__END__

prints:
A big * dog with * eyes.

Anyway, I still think you'd gain more by re-structuring. Clarify the 
process, and someone might be able to suggest a wholistic solution. 
Looks like a case for the power of 'map'. (Master map and you move to 
the next tier ...)

1;


- Bruce

__Bruce_Van_Allen___Santa_Cruz_CA__

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-webcgi-request@macperl.org