Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing arrays from param 1

Status
Not open for further replies.

SPrelewicz

Programmer
Jul 16, 2001
124
US
This started on the mySQL board, but now must be switched to here. Below is a response from a user about checkboxeses:


What I might do with an HTML interface is produce a set of checkboxes with all the possible content, and check by default all those which represent content to which the user is subscribed.

I would also place a set of related hidden HTML form elements in the form to record what which checkboxes were already selected checkboxes at the time the page was produced.

When the form is submitted, another script can scroll through the checkboxes that are selected at the time the form is submitted. For each checkbox submitted, your script can compare the value as it is with the hidden value as it was.

If a checkbox is checked, but there is no related hidden field, your script can assume that the content was previously unsubscribed,and subscribe your user.

If a checkbox is not returned it is because it was not checked. You will have to loop through the hidden fields (those checkboxes already checked) to see which ones were checked previously, but are now not checked. Unsubscribe your user.

If there is a submitted checkbox and a related hidden field both, then the user was already subscribed and elects to remain subscribed. Do nothing.


--------

The problem is, I'm not sure how to do this in Perl. I'm sure I could come up with a hack if I thought long enough, but I doubt it would be the cleanest or quickest way. Can anyone help? Here is the entire thread.

thread436-578830
 
I've been in that situation myself. What I ended up doing which proved easier was to create two list boxes, side by side, instead of checkboxes. The left one with all the possible values, the right one with the list of values that were present for that particular id.

Everything in the right box was highlighted. To remove a value for an id, I unhighlight values, then when I submit it, the script processes deletions from right box, and additions from the left box.

Another reason is because I have a couple friends who know javascript. If I ever get around to tapping their brains, they know how to make it so that you can slide values from the left and right list boxes on the fly without refreshing the page.

--
Andy
 
The easiest way to do this is to convert each array into hash keys and then use the [tt]exists[/tt] operator to see whether a given element is in a list or not. I use the following sub:

Code:
sub AnotB {
    # Return a ref to a list of all members of list @$A which are not in @$B
    # If called in list context, return BnotA as well
    my ( $A, $B ) = @_;
    my ( %B, @result );
    @B{@$B} = (); # set 'exists' in %B for each member of @$B
    foreach ( @$A ) {
        push( @result, $_ ) unless exists $B{$_};
    }
    return \@result unless wantarray;

    # only get here if sub called in array context
    my ( %A, @reverse );
    @A{@$A} = ();
    foreach ( @$B ) { # same thing but backwards
        push( @reverse, $_ ) unless exists $A{$_};
    }
    return( \@result, \@reverse );
}

Yours,



fish


"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
I've just had a colleague tell me that [tt]@B{@$B} = ();[/tt] is unintelligible. At least, that was the gist of what she said.

IMHO, hash slices being uncommon in other languages is no reason to avoid using one of the more powerful features of perl.

It's really not too bad:

* [tt]%H[/tt] is an entire hash.

* [tt]$H{$i}[/tt] is the value from [tt]%H[/tt] corresponding to key [tt]$i[/tt].

* [tt]@H{@a}[/tt] is the list of values from [tt]%H[/tt] corresponding to the list of keys [tt]@a[/tt].

So [tt]B{@$B} = ();[/tt] says: take the list ref $B and use the list that it references ([tt]@$B[/tt]) as keys into hash [tt]%B[/tt]. For each element of [tt]%B[/tt] thus referenced, create it and set it's value to [tt]undef[/tt] - because we've run out of values in [tt]()[/tt] before we've started.

I hope that that is clearer and apologise if it sounds PATRONISING.

Yours,


fish

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top