Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by chuleto1 (Beadle)
on Aug 09, 2002 at 19:06 UTC ( [id://189006]=perlquestion: print w/replies, xml ) Need Help??

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

Please monks you are my only hope:

This piece of code opens a file an dumps contents into array. I want to get rid of possible repeated lines of text. When I run the code, it substitutes repeated elements for only one instance. Yes this is what I want but I do not want the order of the elements to change. Current implementation does not keep correct order when I dump the unique lines into a new file. Please help!

open (FILE,"$tempfile5") || die "Cannot read from $tempfile5\n"; my %hsh; open (OUTF,">$tempfile6") || die "Cannot write to $tempfile6\n"; my @lines = <FILE>; undef @hsh{@lines}; my @lines2 = keys %hsh; foreach my $instance (@lines2) { print OUTF $instance; } close FILE; close OUTF;

Edited: ~Fri Aug 9 22:02:52 2002 (GMT) by footpad: Revised HTML formatting to be more appropriate, e.g. replace <I> tags with <CODE> tags and removed extraneous <BR> tags.

  • Comment on Unique array w/o repeated elements and still keep the original order!
  • Download Code

Replies are listed 'Best First'.
Re: Unique array w/o repeated elements and still keep the original order!
by tstock (Curate) on Aug 09, 2002 at 19:10 UTC
    foreach my $instance (@lines2) { print OUTF $instance unless $hsh{$instance}++; }
    Tiago
    Update: replaced 'if' with 'unless'. thanks robobunny
      i'm assuming it was just a typo, but i think you meant:
      foreach my $instance (@lines2) { print OUTF $instance unless $hsh{$instance}++; }
      as written, it will only print the duplicates.
Re: Unique array w/o repeated elements and still keep the original order!
by TexasTess (Beadle) on Aug 09, 2002 at 19:50 UTC
    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

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-13 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found