Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scanning a list of lists for a string

Status
Not open for further replies.

Loon

Programmer
May 24, 2000
100
GB
Hey folks, long time no see..

I have a list of lists (or array or arrays if you rather ;-)) and am constantly adding stuff to it. However, I don't want to add duplicates. Therefore I want a quick and easy way of searching all the entries in the matrix for the string I am about to add.

The script will eventually check through a C header, grab all the
Code:
#include
statements, add them to a to-do list. Once a C header is parsed, the ToDo list is consulted for the next header file to scan through. In theory then at the end of the process all nested includes from the original header file should have been processed...

So if
Code:
@list
contains a list of header files (a.h, b.h, c.h...) and the
Code:
@{$ToDoList[$curr_lvl]}
contains lists of files to check through in the future. I want to ensure that the push I do onto ToDoList does not already occur in the ToDoList (not necessarily at this level - but anywhere in the matrix).

Code:
foreach $item (@list)
{
    print "\tAdding $item\n";
    push @{$ToDoList[($curr_lvl + 1)]}, $f;
}

Cheers
Loon
 
foreach $item (@list) {
print "\tAdding $item\n";
my $found = 0;
foreach $todo_item (@ToDoList) {
if ($todo_item eq $item) {
$found = 1;
last; ### break out of foreach $todo_item (@ToDoList)
}
}
if (! $found) {
push(@ToDoList, $item);
}
}

I haven't tested it, but I think it will work.

The other thing you can think about is using a hash to store your ToDoList, since a hash is key/value it won't allow duplicates. If you aren't familiar with hashes, read up on them - on *nix you can do "perldoc perldata".

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Cheers folks,

I was kinda looking for a quicker way than the foreach to do it, and I was also thinking about hashes. In the end though to speed up the development I opted for:

Code:
$_DONE_THIS = "";

and then:

Code:
foreach $item (@list)
{
    if ($_DONE_THIS !~ /$item/)
    {
        print "\tAdding $item\n";
        push @{$ToDoList[($curr_lvl + 1)]}, $item;
        $_DONE_THIS .= $item . "#";
    }
}

The # separator is not necessary of course but makes it easy to read DONE_THIS if required - it also stops some fake matches. This method isn't that pretty but seems pretty fast.

The reason I asked on here was I thought that perl looked at Arrays as a long list of scalars and hence I thought there might be something along the lines of:

Code:
if (@myList =~ /search_term/) { etc...

cheers for all the help
Loon

 
I completely feel the frustration here.

In PHP, we can use the in_array() function. In Perl, it is not quite so simple.

I am also in an agreement about utilizing the limits of a hash since they won't allow duplicates. The only thing to remember is that it is the key that cannot duplicate, so if we did this, we can do something like:

%list("a.h" , 1,
"b.h" , 1 );

Chad. ICQ: 54380631
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top