(don't top-post; see responses in context below...) > -----Original Message----- > From: Nazary, David [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, April 23, 2002 1:12 PM > To: 'Nikola Janceski'; '[EMAIL PROTECTED]' > Subject: RE: unexpected results with grep > > > instead of $_ I used $name but nothing was found by grep. > What do you mean > by another variable? > > > -----Original Message----- > From: Nikola Janceski [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, April 23, 2002 9:40 AM > To: 'Nazary, David'; '[EMAIL PROTECTED]' > Subject: RE: unexpected results with grep > > > inside grep $_ is a special var assigned to each value of the array. > use another var for the //; > > > > -----Original Message----- > > From: Nazary, David [mailto:[EMAIL PROTECTED]] > > Sent: Tuesday, April 23, 2002 12:29 PM > > To: '[EMAIL PROTECTED]' > > Subject: unexpected results with grep > > > > > > Hi, > > > > I am trying to use grep in a simple perl script to find all > > the lines in a > > list (@labels) that contain certain names in a list (@names). > > The problem is > > that grep finds all the lines in @labels for every name in > > the @names. here > > is the script: > > > > >>>>>>>>>>>> > > > > open(NAMES, "names"); > > open(LABELS, "labels");
Need error checking. > > @labels = <LABELS>; > > @names = <NAMES>; Probably need to chomp() at least the names. > > > > foreach $name (@names){ > > open(NEW, ">$name"); #create a file with the name > > from @names Need error checking. > > @labeled_names = grep (/$_/, @labels); grep(/$_/, @names) is equivalent to grep($_ =~ /$_/, @names), which is not what you want. If you want to find the labels containing the current name, you need something like: grep /$name/, @labels; But I don't see you chomp() $name anywhere, so it contains a newline, which may or may not cause a problem, depending on what you're trying to accomplish. > > print NEW @labeled_names; > > @labeled_names = ""; #clear the list for the > > next name This statement is unnecessary. Also, it doesn't clear the list, if by that you mean empty it of all elements. To do that, you can use: @labeled_names = (); Your statement results in an array with one element, the string "". > > close NEW; > > } > > close NAEMS; NAMES is misspelled. > > close LABELS; > > > > >>>>>>>>>>>>> > > > > any idea what is not write here? It would help to post some representative data, and show what you expect to happen. It's hard to answer the question "Why doesn't this work?", when you haven't defined "work". -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]