in reply to @array1 vs @array2

OK, here's some example-arrays as well.

@array1 = ( "1,Larry Wall" , "2,Douglas Adams"); @array2 = ( "1,Programing Perl,1", "2,The Hitchhikers Guide to the Galaxy,2", "2,Mostly Harmless,2"); foreach (@array1) { ($id,$name) = split( /,/, $_); print "The author is $name"; $no = grep { m/,$id$/ } @array2; print "We have $no books by this Author\n"; }
--
Brigitte    'I never met a chocolate I didnt like'    Jellinek
http://www.horus.com/~bjelli/         http://perlwelt.horus.at

Replies are listed 'Best First'.
Re: Re: @array1 vs @array2
by Anonymous Monk on Mar 19, 2001 at 22:23 UTC
    I tried the first example and it only printed the customer name with a count of 0.
    open(FILE, "data.txt"); open(FILE2, "info.csv"); @data = <FILE>; @data2 = <FILE2>; ## TEST CODE foreach (@data) { ($id,$name,$ref) = split( /,/, $_); print "The customer is $name"; $no = grep { m/,$id$/ } @data2; print "We have $no docs by this $id\n"; } close(FILE); close(FILE2); exit; ## END TEST CODE

    Am I not thinking clearly(as I usually don't) on this or what?
    Here's the output:
    he customer is Customer1 coWe have 0 docs by this 1
    The customer is Customer2 coWe have 0 docs by this 2
    The customer is Customer3 coWe have 0 docs by this 3
      Remove the comma from the regex.

      But really, do not do it this way. Given that you are processing the entire contents of @array2 for each element in @array1, you have suddenly created an O(n^2) algorithm. This will hurt very quickly if either @array1 or @array2 gets very large. Especially since I would expect @array2 >> @array1.

      Try the one of the two hash solutions presented ( either one does basically the same thing ).

      mikfire