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

nononono..no..nono..

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 :)


In reply to Re: See if arrays match by PhiRatE
in thread See if arrays match by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.