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

I have six lists with numbers from 1 to 50. I have a variable which takes values from 1 to 50. I want to find the list which holds that variable and the number of that list (1-6). How can I either loop over the lists or create a reference to the lists?

Replies are listed 'Best First'.
Re: search multiple lists
by mlh2003 (Scribe) on Mar 04, 2005 at 00:34 UTC
    One solution would be to grep each list and return with 'found' if the variable was found in the list. I am assuming you have an array of arrays here, and $variable contains the number you are searching for in each list:
    for $i (0 .. $#@AoA) { if (grep(/$variable/, @{$AoA[$i]})) { print "List " . $i+1 . "contains $variable.\n"; } }
    _______
    mlh2003
      Thank you. It took me a while to get it going and not all of your suggestions worked but this is how the relevant part of my code looks like now.
      @tect1=(1,5,6,8,16,20); @tect2=(1,7,10,12,13,14,15,18,19,22,23,24,46); @tect3=(1,2,3,9,10,11,17,21,24,39,46); @tectlist=(\@tect1, \@tect2, \@tect3); $variable=19; #as an example for $i (0..$#tectlist) { if (grep (/$variable/, @{$tectlist[$i]})){ print" List"; $listnumber=$i+1; print" $listnumber"; print " contains $variable.\n"; } }
        A few comments. One thing is any time you see a few variables whose names end in numbers, especially small sequential numbers (@tect1, @tect2, @tect3) you're usually better off seeing if you can't make them into an array. Nine times out of ten, you end up wanting to treat them that way anyway. So in this case you could make an array of arrays in the first place.

        The other problem I see is your grep. You're using the number you're looking for as a regexp. That works sometimes, but if you look for, say /1/ the list (7,10,12) it'll be found (because the string "10" contains the string "1"). So here's how I would have written this part.

        @tectlist = ( [1,5,6,8,16,20], [1,7,10,12,13,14,15,18,19,22,23,24,46], [1,2,3,9,10,11,17,21,24,39,46], ); $variable=19; for my $i (0..$#tectlist) { if (grep {$_ == $variable} @{$tectlist[$i]}){ my $listnumber = $i+1; print "List $listnumber contains $variable.\n"; } }
Re: search multiple lists
by tirwhan (Abbot) on Mar 04, 2005 at 01:39 UTC
    This may not be the solution you're looking for, but you could turn the problem around. Instead of populating the lists, use the numbers as keys of a hash and the lists as the hash values. For example:
    # 13 is in list 1 # 23 is in list 5 and 6 # 42 is in list 1,3 and 5 my %lists; @lists{13,23,42}=([1],[5,6],[1,3,4]); # Find which lists contain the values for my $i (13,23,42) { print "Value $i found in list $_\n" foreach (@{$lists{$i}}) }

    If you've already got the lists as arrays (or an array of arrays) this is probably not worth doing unless you need to find multiple values.

    Perhaps more importantly the solution should make sense in the context of your code (which is hard to tell from your description), otherwise my solution makes the code harder to read/maintain and a different solution would be preferable, albeit slightly slower (since you need to search through all the lists).

      Thank you for your suggestion. You were right that I was not after a solution like that but I will keep it in mind.
Re: search multiple lists
by Fletch (Bishop) on Mar 04, 2005 at 03:13 UTC

    Smells like homework.

    require 'pp' class Array def shuffle!( ) each_index {|i| j = rand(i+1); self[i], self[j] = self[j], self[i] +} end def shuffle( ) self.dup.shuffle! end end lists = (1..6).inject( [] ) { |m,o| m << [] } ( 1..50 ).to_a.shuffle.each { |x| lists[ rand(6) ] << x } target = Integer( rand( 50 ) + 1 ) pp lists list_no = nil lists.each_index { |i| list_no = i if lists[i].any? { |x| x == target +} } puts "#{target} is in list #{list_no}"
      Sorry, I am more of a 'baby perl' person. Your code looks a little like Egyptian hierroglyphs to me. Maybe one day I will be able to decipher. Thanks for your response. It's good to know that there is still heaps to learn! 8-)
        A book about Ruby may help you decipher... :-)
Re: search multiple lists
by Roy Johnson (Monsignor) on Mar 04, 2005 at 00:25 UTC
    Do you have any code, or do you only have a notion of what you want to do?

    Caution: Contents may have been coded under pressure.
Re: search multiple lists
by ssk (Initiate) on Mar 04, 2005 at 11:28 UTC
    try this out...
    @list1 = qw(1 3 44 5 50 6 34 23); @list2 = qw(11 35 4 50 9 6 34 23); @list3 = qw(23 1 23 4 50 6 34 23); @list4 = qw(43 1 33 34 50 9 6 34 23); @list5 = qw(3 1 45 3 4 50 6 34 23); @list6 = qw(14 54 3 4 50 6 34 23); $total = [\@list1, \@list2, \@list3, \@list4, \@list5, \@list6]; $search = '9'; map{$tmp++ , map{ print "found in list $tmp \n" if $_ eq $search } @ +$_ }@$total
    - Sampath