husoft has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: huge problem!
by Joost (Canon) on Sep 10, 2002 at 15:02 UTC
    First of all see How (not) to ask a question.

    You need something like:

    my %seen; for (@input) { $seen{$_}++ && warn "$_ already entered\n"; }
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: huge problem!
by rdfield (Priest) on Sep 10, 2002 at 13:54 UTC
    ...and the code that produces this problem is...

    rdfield

      my $in=join (", ", (map { $_ = "'$_'" } @one)); $sql="SELECT * FROM sistema WHERE codigo_inscripcion IN ($in) and mat +ricula='$matricula'"; $pdbh=$dbh->prepare($sql); $pdbh->execute(); # stuff here! while (my $ref = $pdbh->fetchrow_hashref()) { if($ref->{'mon'} != ''){$mon="Mon$ref->{'mon'})} if($ref->{'tue'} != ''){$mar="Tue$ref->{'tue'}"} if($ref->{'wed'} != ''){$mie="Wed$ref->{'wed'}"} if($ref->{'thu'} != ''){$jue="Thu$ref->{'thu'}"} if($ref->{'fri'} != ''){$vie="Fri$ref->{'fri'}"} if($ref->{'sat'} != ''){$sab="Sat$ref->{'sat'}"} if($ref->{'sun'} != ''){$dom="Sun$ref->{'sun'}"} } $pdbh->finish(); exit; if ($mon ne ''){ $lun=~ /([A-Za-z]*)([0-9]*)\/([0-9]*)/; for($z=$2;$z<$3;$z++) { push (@soft, "$1$z"); } push (@soft, "$1$3"); print @soft; exit;
      Im trying with this... this is actually printing only 1 of
      the selections how I want... but I think this code stinks!
      If I select more than one row, it overwrites the value of
      $mon and only shows me the last... I dunno how to do
      this! Please HELP!
        You might want to start with
        use strict; use warnings; use diagnostics;
        and see what happens - I can see at least one typo that is causing you problems.

        rdfield

        You could change the while loop to something like:
        my %seen; while (my $ref = $pdbh->fetchrow_hashref()) { for (qw(mon tue wed thu fri sat sun)) { my $result = lcfirst($_).$ref->{$_} if $ref->{$_}; if ($seen{$result}++) { warn "$seen found already"; } else { push @results,$result; } } } print join("\n",@results);
        That should print out the results with duplicates removed in the format you seem to want.
        -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: huge problem!
by blssu (Pilgrim) on Sep 10, 2002 at 14:21 UTC

    Your title description is very poor. It's too vague. Besides, the problem is not "a" huge problem, it's "your" huge problem. Please use something a little more descriptive next time.

    I'm not exactly sure what you're asking. I assume you know how to query your database, and how to insert those results into another table. That only leaves the uniqueness check. Try using a hash to enforce uniqueness. Something like this:

    use strict; my @values = qw(mon10 mon11 mon12 wed15 wed16 wed17 wed18 mon9 mon10 m +on11); @values = sort keys %{({ map { $_ => 1} @values })};

    If you don't mind using an explicit temporary hash, you can use this instead:

    my %uniq = map { $_ => 1} @values; @values = sort keys %uniq;

    Lastly, your database itself might be able to guarantee uniqueness of the rows. I know Oracle has the keyword DISTINCT that eliminates duplicates. You might want to check MySQL for something similar. (You'd also need to expand the ranges in the database too. Oracle can do that, but I'm not sure about MySQL.)