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

Re: [MacPerl-WebCGI] loops 'n' lists [was Referencing a Hash element...]



Hi Bruce,

Thanks for the reply. It is very much apprciated.

I haven't had a chance to fully digest your code yet,
but even a quick glance is enough to see that it is a
lot more compact. I have already rewritten sections of
my code since re-examining the structure after your
previous e-mail. In my defence, it works exactly as
intended, but I am never happy with code that just
works. It has to be compact too. I must learn to leave
it alone once it does the job.

Thanks again for your time, and for intoducing me to
the 'map' function. =)

Best regards,

Richard


--- Bruce Van Allen <bva@cruzio.com> wrote: > See
previous msg from Richard Smith, 8:52 PM
> 12/28/00.
> 
> Richard -- Here's an illustration of the use of
> loops and lists to 
> process your admin form input.
> 
> First, the code below populates the %Form hash, to
> simulate form 
> input. Then using map, we make a set of lists of the
> keys of %Form 
> whose elements you want to process. Take some time
> to examine just 
> the map construct; there's a lot going on there.
> (The final 'or ()' 
> ensures that @admin_keys doesn't get any blank
> elements for keys that 
> don't match the ones of interest.)
> 
> The code goes on to use the lists of keys to make
> hash slices, which 
> are then looped over for processing. Along the way,
> things get 
> printed out so you may see what's transpired.
> 
> To simulate your error handling subroutine, my code
> just accumulates 
> the errors in the variable @errors. Also, as you
> correctly do in your 
> original code, $& must be segregated within a block,
> and that is why 
> the last loop in my code has the extra enclosing
> braces.
> 
>    foreach ( @Form{@description_keys,
> @linktext_keys} ) { {
>      ...
>    } }
> 
> 
> After you've grokked this stuff, here are some
> questions:
> 
> a. Why chomp?  I threw in a return in
> $Form{description1} to show 
> that the chomping works, but if your input is from
> HTML text fields, 
> there won't be any line endings (unless you use
> <textarea>).
> b. The way you've patterned your capture of illegal
> characters, 
> you're getting them in contiguous clumps, replacing
> with a single 
> '*'. Might be what you want, but might not.
> c. Why substitute? Your original code, replicated in
> mine, isn't 
> really meant to *correct* anything, just to spot
> errors and send them 
> to your error handling subroutine. Just match?
> d. Employing $& is misleading when a given item has
> more than one 
> illegal; only the last one found will be in $& when
> you insert it 
> into your error alert string.
> 
> Anyway, here's the code, followed by the output
> (beware of email line 
> breaks), and I'm going to finish lunch...
> 
> 1;
> 
> - Bruce
> 
> 
> #!perl -w
> 
> # Simulated form input:
> my %Form;
> $Form{_submitter} = 'Ralph';
> $Form{_date} = '2001/01/17';
> $Form{_comment} = 'Here are my changes...';
> $Form{linktext1} = 'Big Ideas';
> $Form{description1} = ' %hings we are planning ...
> ';
> $Form{location1} = '/site/big.html  ';
> $Form{linktext2} = '  Small Ideas ';
> $Form{description2} = 'Some other $%^&* stuff.';
> $Form{location2} = '/site/small.htm';
> $Form{linktext3} = 'Your Ideas (??)';
> $Form{description3} = ' &Useful! ideas from our
> clients ...';
> $Form{location3} = ' /site/client.html  ';
> 
> # Some arrays:
> my @errors;
> my (@linktext_keys, @description_keys,
> @location_keys);
> 
> # Populate the arrays from the hash keys:
> my @admin_keys = map {
> 	/linktext/ and push @linktext_keys, $_ and $_
> 	  or
> 	/description/ and push @description_keys, $_ and $_
> 	  or
> 	/location/ and push @location_keys, $_ and $_
> 	  or
> 	()
> } sort keys %Form;
> 
> print "Before:\n";
> foreach (sort keys %Form) { printf "%-14s %s\n", $_,
> $Form{$_} }
> 
> 
> # To process all of the admin elements of %Form,
> # make a list from a hash slice with @admin_keys as
> the indices
> # 1. remove line endings
> chomp @Form{@admin_keys};  # chomp works on all
> elements of a list
> # 2. trim whitespace
> foreach ( @Form{@admin_keys} ) {
>      s/^\s*(.*?)\s*$/$1/
> }
> 
> # Process only 1 set of elements
> # 3. single '.' at end of descriptions
> foreach ( @Form{@description_keys} ) {
>      s/\s*\.*$/./
> }
> 
> # Process >1 sets of elements
> # 4. replace non-alphas with '*' and
> # 5. record errors
> foreach ( @Form{@description_keys, @linktext_keys} )
> { {
> 	s/[^a-zA-Z .]+/*/g;
> 	push @errors, <<ERR if $&;
> The description in line
>    $_
> contained an illegal character '$&'
> ERR
> } }
> 
> print "\nAfter:\n";
> foreach (sort keys %Form) { printf "%-14s %s\n", $_,
> $Form{$_} }
> print "\nErrors:\n", join "\n", @errors;
> 
> 
> __END__
> 
> 
> Before:
> _comment       Here are my changes...
> _date          2001/01/17
> _submitter     Ralph
> description1    %hings we are planning ...
> 
> description2   Some other $%^&* stuff.
> description3    &Useful! ideas from our clients ...
> linktext1      Big Ideas
> linktext2        Small Ideas
> linktext3      Your Ideas (??)
> location1      /site/big.html
> location2      /site/small.htm
> location3       /site/client.html
> 
> After:
> _comment       Here are my changes...
> _date          2001/01/17
> _submitter     Ralph
> description1   *hings we are planning.
> description2   Some other * stuff.
> description3   *Useful* ideas from our clients.
> linktext1      Big Ideas
> linktext2      Small Ideas
> linktext3      Your Ideas *
> location1      /site/big.html
> location2      /site/small.htm
> location3      /site/client.html
> 
> Errors:
> The description in line
>    *hings we are planning.
> contained an illegal character '%'
> 
> The description in line
>    Some other * stuff.
> contained an illegal character '$%^&*'
> 
> The description in line
>    *Useful* ideas from our clients.
> contained an illegal character '!'
> 
> The description in line
>    Your Ideas *
> contained an illegal character '(??)'
> 
> 
> 
> 
=== message truncated ===


__________________________________________________
Do You Yahoo!?
Yahoo! Photos - Share your holiday photos online!
http://photos.yahoo.com/

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