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!

Search script

Status
Not open for further replies.

belkira

Technical User
Joined
Mar 19, 2007
Messages
9
Location
US
Hi, sorry if this does not make much sence. I am new to perl and programming in general but was thrown into this expected to hit the ground running.

I need to write a script that will read information from one file then compare it information in another file and output the outcome into still a different file.

So to explain a little more, I have a list of "VIP" users and a list of users being affected by a problem. I need to search the list of users having a problem for anyone on the VIP list, and then have those name put into a different file.

Possible?
 
Possible certainly, though as you're new to programming in general you have a lot of new ground to cover.

Have you decided on an overall approach? If you haven't perhaps you could have a think about this, it will work best if you don't have many VIP's in your list - not more than 20, say.

[li]read your VIP file and build a search string[/li]
[li]for each line in your problems file[/li]
[li][tab]if the line matches your search string[/li]
[li][tab][tab]write the line to the results file[/li]


Mike

Hardware is that part of a computer which, when you remove electrical power, doesn't go away.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
There are actually about 110 people on the vip list and over 2000 on the problems list.

Would your approach still work or should I look to a different solution?
 
Someone will do this better than me.. but

@list = your 2000 users
@vip2 = you vips users

You will still need to read in files and set up your out bound file.

for $vip (@vips) {
if (grep /$vip/, @list) {
print "$vip\n";
}
}
 
Or without using grep
Code:
for $vip (@vips) {
 for $name (@list) {
  if ($vip eq $name) {
   print "$vip\n";
  }
 }
}

using grep could cause you problems... you know if you had user names that were close (like ted and ted1)
 
Thanks,

Now I hate ask, but as I said I am still new to all this, is that a complete program? Can I copy/paste is and have it work? just name the file something.pl
 
Even as a new perl programmer or new programmer in general, you must realize nobody can write you a working program for unknown data and an unknown environment. Post some sample data from your VIP file and the other file and explain the data structure if necessary. Will this be a command line/shell program or CGI program? Then someone can start to help you with some code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It will be command line, no point making it to fancy as of yet.

The data in both files is in the format of:

FirstName LastName
FirstName LastName

So one name per line, both fist name and last name on that line. No headers or anything like that, just plain text file.

What more info can I provide?
 
Here's some code you can just paste in and run
Code:
#!/usr/bin/perl

use strict;
use warnings;

my %vips;
my $vipfile = shift;

open(VIPS, $vipfile) or die "Can't open VIP file $vipfile:$!";

while (<VIPS>) {
    chomp;
    $vips{$_}++;
}

close(VIPS);

while (<>) {
    chomp;
    print $_, "\n" if exists $vips{$_};
}
but it only works if vips.txt looks like this
Code:
vip1
vip3
vip2
vipx
and errors.txt looks like
Code:
user1
userx
vip1
user9
vip3
and you run it like
Code:
perl vipfinder.pl vips.txt errors.txt

[i]or[/i]

./vipfinder.pl vips.txt errors.txt
If it does, you're in luck...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Thank you very much!

As one final request on this, how can I make it output to a txt file instead of the screen?
 
Oh and make it so it is not case sensitive....
 
ok I figured out how to output to a text file, so now the only remaning issue is how can I make everything lower case before the file comparison
 
while(<VIPS>) {
chomp;
$tmp = lc $_;
$vips{$tmp}++;
}

while (<>) {
chomp;
$tmp = lc $_;
print $tmp, "\n" if exists $vips{$tmp};
}

 
I tried that and I get this error:

Global symbol "$tmp" requires explicit package name at compare.pl
 
That's because we're using strict. If you make a new variable like $tmp, you have to declare it with my. You can either do this, or just dispense with the intermediate variable $tmp altogether.
Code:
while(<VIPS>) {
    chomp;
    $vips{lc($_)}++;
}

while (<>) {
    chomp;
    print $_, "\n" if exists $vips{lc($_)};
}


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Tnanks for all your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top