Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Unique array w/o repeated elements and still keep the original order!

by TexasTess (Beadle)
on Aug 09, 2002 at 19:50 UTC ( [id://189018]=note: print w/replies, xml ) Need Help??


in reply to Unique array w/o repeated elements and still keep the original order!

you don't say if the repeated lines will be in succession(one right after the other) or if they can be spread out through the file. If it's random...then this is what i'd use....
my @lines=(); open (FILE,"$tempfile5") || die "Cannot read from $tempfile5\n"; my $found = 0; while(<FILE>){ my $line_holder = $_; #DOH, should have had this here foreach my $val(@lines){ if($val eq $line_holder){$found = 1;} } if($found){ $found = 0; next; }else{ push @lines, $line_holder; } }


TexasTess
"Great Spirits Often Encounter Violent Opposition From Mediocre Minds" --Albert Einstein
  • Comment on Re: Unique array w/o repeated elements and still keep the original order!
  • Download Code

Replies are listed 'Best First'.
Re: Re: Unique array w/o repeated elements and still keep the original order!
by jynx (Priest) on Aug 09, 2002 at 20:17 UTC

    That won't work how you expect it to...

    Particularly, what's happening to $_? It's getting set by the while loop and then forgotten about, which is to say that you'll not process all of the lines. Oops. How about this:

    while (my $line_holder = <FILE>) { foreach etc...
    Or alternately you could check for file EOF in the while condition like so:
    while (! eof(FILE)) { my $line_holder = <FILE>; foreach etc...
    Either way should fix the problem...

    jynx

      I'm not sure what you mean by
      while (my $line_holder = <FILE>) { foreach etc...

      That is certainly != to what I wrote....
      while(<FILE>){ my $line_holder = $_; #GOTCHA foreach my $val(@lines){

      Which is something I do in tons of code I have written at work and it works fine?? Perhaps I am missing something?
      TexasTess
      "Great Spirits Often Encounter Violent Opposition From Mediocre Minds" --Albert Einstein
      Waiting to see just how fast this makes it to an all time negative record...and wondering if any of the downvoters think I care :-)
        That is certainly != to what I wrote....

        Exactly the point. In your code, each iteration of the loop assigns the next row to $_ (your while statement), then assigns the following line to $line_holder. The line assigned to $_ is never handled, so it just get's "skipped."

        In any case, couldn't the whole process be reduced to

        my %seen = () ; my @uniq_rows = grep { !$seen{$_}++ } <FILE> ;

        ?


        _______________
        DamnDirtyApe
        Those who know that they are profound strive for clarity. Those who
        would like to seem profound to the crowd strive for obscurity.
                    --Friedrich Nietzsche

Log In?
Username:
Password:

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

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

    No recent polls found