Hi Folks,

In need of some help please.

Scenario:- I have a csv file with many lines of url filter strings (may include asterisks eg "http://*.google.com*/*"). I read each line, want to add it to a list of some form (array, hash, whatever) to be used in a loop later. I only want strings added to this list if they dont already exist in there. Of the thousands of filters i read in, there may only be 5 or 10 unique ones. I want to create this list as efficiently as possible. I am using Text::Csv::Slurp to read in the csv.

input file sample:-

parm1,http://*.google.com*/*,parm3 parm1,http://*.google.com*/*,parm3 parm1,http://*.yahoo.com*/*,parm3 parm1,http://*.google.com*/*,parm3 parm1,http://*.gmail.com*/*,parm3 parm1,http://*.yahoo.com*/*,parm3 parm1,http://*.google.com*/*,parm3

Desired output list in whatever format lets me easily loop through later (array, hash, whatever) "http://*.google.com*/*", "http://*.yahoo.com*/*", "http://*.gmail.com*/*"

Type of code i have tried :-

if (grep {$time_url_hits->[$i]->{url}} @url_list){ #print "found \n"; } else { print "adding url " . $time_url_hits->[$i]->{'url'} . " to url arr +ay \n"; push (@url_list,$time_url_hits->[$i]->{'url'}); ... <other processing> ... <then later> foreach (@url_list) { <do stuff for each unique filter string>

for some reason it only adds the very first filter string in the array. Im guessing this is to do with the asterisks and grep somehow. Anyway, any assistance on achieving this elegantly would be appreciated

Regards Rep


In reply to ensuring unique elements in a list by repudi8or

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.