Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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 ( #189027=note: print w/ replies, xml ) Need Help??


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


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


Comment on Re: Re: Unique array w/o repeated elements and still keep the original order!
Select or Download Code
Re: Re: Re: Unique array w/o repeated elements and still keep the original order!
by TexasTess (Beadle) on Aug 09, 2002 at 20:34 UTC
    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
        Er just a thought but any time ive written that code ive ended up changing it to
        my @unique=map{chomp; $_{$_}++ ? () : $_} <FILE>;
        or something like it (i dont always use %_ but sometimes i do, blame ABIGAIL-II ;-)

        To be honest i think that in my perl career ive only used a grep a few times. map seems to be so much more useful in that it can do grep()s job, and more as well!

        Yves / DeMerphq
        ---
        Software Engineering is Programming when you can't. -- E. W. Dijkstra (RIP)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-12-26 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls