Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: See if arrays match

by PhiRatE (Monk)
on Jul 23, 2002 at 22:00 UTC ( #184620=note: print w/replies, xml ) Need Help??

in reply to See if arrays match

$b = 0; while ( @slog = $sth->fetchrow_array ) { $b++; $info = param($b); ($a,$b) = split(/=/, $info); push(@info,$b) }

I'm not yet entirely sure what you're trying to achieve by that code, but I would seriously consider re-writing it. You don't *appear* to have used 'strict' or 'warnings', and from the looks of things its a CGI script and you don't have 'taint' enabled either. These are important things, you haven't used my() to scope any of the variables and this will cause problems later on when you accidentally overwrite the $b somewhere else.

Secondly you use fetchrow_array. This is fine *if* your SQL statement is carefully constructed so that you have specified the order or the fields coming out. If you've just used "select * ..." then you're in trouble if you ever alter your table schema. Instead consider fetchrow_hashref which will give you nicely named fields ($slog->{'row_id'} ..).

Thirdly you're doing something really strange with $b there. Your first step is to set up $b as a counter, ok, thats cool. Then for each row in the result you increment $b (as if it were a counter), use $b as a param() reference, obviously to get some number-named form element (thats fine), but then weirdly you over-write $b with the value after the = sign in the split and push it on to an array (that hasn't been visibly initialised to empty or scoped but you may not have included that).

In addition, you're using $info and @info in the same piece of code. Thats not good for readability at all.

Now, back to your question, I'm assuming this need-to-know-about-duplicates is to do with the weirdness that is going on with $b. You want to error if you're likely to get into a loop with $b being the same value it was previously (bearing in mind that the loop will come to a halt anyway due to the rows of the query running out).

The easiest way to do that is to use %info instead of @info and check it each time, as so:

if (defined($info{$b})) { die "Duplicate reference"; } $info{$b} = $b;

Then later when you need the list, use keys(). Alternatively if you need them in order, you would be advised either to use an OrderedHash module (there's one on CPAN) or an array and a hash, use the hash for checked and the array for order.

To be honest, I think you would be best advised to instead post to perlmonks the entirety of your problem, rather than this little section. It seems like whatever it is, you're doing it the hard way and we may well be able to help more :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://184620]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2023-05-31 03:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found