Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
$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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-23 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found