Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: finding unique items in an array, from a text file

by andye (Curate)
on Jan 13, 2009 at 19:28 UTC ( #736054=note: print w/replies, xml ) Need Help??

in reply to finding unique items in an array, from a text file

hiya - looks a bit overcomplicated, should be really simple. IMHO you don't need @lines, @uniq and %seen... only need one hash to do this.

I'd prefer to do something like this (off the top of my head, code not tested):

# open the file into FH my %uniq; $uniq{$_} = 1 while (<FH>); print join "\n", keys %uniq; # close the file

Hope that helps. All the best.

Replies are listed 'Best First'.
Re^2: finding unique items in an array, from a text file
by johngg (Abbot) on Jan 13, 2009 at 23:27 UTC

    I think there might be a couple of problems with your code.

    • You join the output with \ns but you did not chomp the input so you will get extra blank lines.
    • Hash keys are unordered so the line order of the original file will be scrambled. This may or may not be an issue.

    The grep solutions suggested might be better if line order is to be preserved. Something like (again, not tested).

    ... my %seen = (); print grep ! $seen{ $_ } ++, <$fh>; ...



      All good points. ;)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://736054]
[Discipulus]: ah! so you mean if i need a static image i can use Twig, if on the fly better css selector and mody attrs (jquery?), rigth?
[LanX]: not sure what you guys are taking about, but a DOM manipulation in JS shouldn't be difficult
[Corion]: Discipulus: Yeah, that's what I would try (but so far, I can't even change the colors manually in the SVG, which is why I gave up ;) )
Discipulus bad sign..
[Discipulus]: italian doc says to modify somehow the initial SVG before using it with D3.js
[Discipulus]: LanX the fact is that my js ability is even poorer than my Perl one..
[LanX]: oh its negative? ;P
[Discipulus]: my warmest comprehension karlgoethebier ;=(

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2017-03-23 09:48 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (285 votes). Check out past polls.