She-wolf has asked for the wisdom of the Perl Monks concerning the following question:

I have a flat text file "database" style program for manipulating a schedule.
I need to limit the number of fields that are displayed for modification though. The code section follows(not the whole subroutine)

sub print_modify_page{ ($key,@field_vals) = split(/\|/, $results[0]); $fs="<FONT SIZE=2 FACE=ARIAL>"; $fc="</FONT>"; &mail($field_vals[0]); print $q->start_html(-TITLE=>'Modify Record',-BGCOLOR=>'white'), $q->start_form; print<<HTML; <CENTER><FONT SIZE=5 FACE=ARIAL> Modify Record </FONT></CENTER> <HR WIDTH=75%> <INPUT TYPE=HIDDEN NAME=key value="$key"> <CENTER> <TABLE BORDER=1 CELLSPACING=0> HTML $x=0; foreach $field (@fields){ print<<HTML; <TR BGCOLOR="e0e0e0"> <TD>$fs<B>\u$field:</B>$fc</TD> <TD><INPUT TYPE=TEXT NAME="$field" VALUE="$field_vals[$x]" SIZE= +40></TD> </TR> HTML $x++; } # End of foreach.

I need to keep the program from displaying the last field for modification. Please help. Thanx

She-wolf
"Wha? I don't get it."

Replies are listed 'Best First'.
Re: Restricting fields
by adamsj (Hermit) on Aug 30, 2000 at 02:53 UTC
    Here's a possibility:
    $x=0; $dont = pop @fields; foreach $field (@fields){ # and so on...do something with $dont or don't } push(@fields, $dont);
    You could write a C-style for loop, or I guess you could do
    last if $x == $#fields -1;
    but that's a little inefficient.
Re: Restricting fields
by jreades (Friar) on Aug 30, 2000 at 03:34 UTC

    One thing I'm a little unclear about -- is @field_vals (which I don't see initialized anywhere) the same as @fields?

    Depending on that, you could take a couple of approaches... Here's one:

    $x = 0; for (0..($#fields-1)){ print<<HTML; <TR BGCOLOR="e0e0e0"> <TD>$fs<B>\u$field:</B>$fc</TD> <TD><INPUT TYPE=TEXT NAME="$fields[$_]" VALUE="$field_vals[$ +x]" SIZE=40></TD> </TR> HTML $x++; }
Re: Restricting fields
by turnstep (Parson) on Aug 30, 2000 at 16:16 UTC

    You could also modify your for loop, which has the advantage of specifying exactly which array elements to bypass, as well as skipping multiple things. Or just check for the last item, or do both:

    $x=0; for $field (@fields) { next if $field =~ /[modified|META|WhateverYouWant]/; last if $x==$#fields; ## or just this print<<"HTML"; <TR BGCOLOR="#e0e0e0"> <TD>$fs<B>\u$field:</B>$fc</TD> <TD><INPUT TYPE="TEXT" NAME="$field" VALUE="$field_vals[$x]" SIZ +E="40"></TD> </TR> HTML $x++; } # End of foreach.
Re: Restricting fields
by wardk (Deacon) on Aug 30, 2000 at 05:08 UTC

    Disclaimer: I do not endorse this method over others I see posted, this is just to share an experience :-)

    I had a project where I had to "rewrite" a part of the page that had been outputted (this widget at the top MUST change, said management).

    my solution was to just save the page off in an array, and going back and massaging the area in question. it was a kludge, but I couldn't redesign this "legacy" app for something that could again change do to some new marketing gadget. In this case in particular, the site saw no performance hit, of course it was only used by a specific customer base on a casual basis, I wasn't facing a million hits a year, let alone a day. Good luck!