On Tuesday, April 30, 2002, at 04:07 PM, Johnson, Shaunn wrote:
> Howdy: > > I'm looking for examples of how to use element 0 > in one array as a search pattern and looking in > other arrays for that pattern. Here's what I have > so far ... > > [snip code] > #!/usr/bin/perl -w > use diagnostics; > > # test to read two files into two arrays > # do a search for each *FIRST* element in the array per line > # and print the matching line in the second file into a > # new file > > > $file="command.txt"; > $file2="command2.txt"; > > @array=<file>; > @array2=<file2>; don't see you opening the files. something like: open (FILE1, $file) or die "Unable to open: $!"; and same for FILE2. > open (INFILE, ">/tmp/infile.txt") or die "Unable to open: $!"; > while (<file2>) { > if (m!$array[0]!) { > print INFILE $_; > } > } > close INFILE; this doesn't work if you have already read file2 into @array2. looks like you don't mean to read it above. > [/snip code] > > What I'm trying to do is use the first element in the first > file (which *should* be an array) and look for it's match > in the second array. If I find the match, I print that > line. if you read the file into an array, each element is a line/string, not an array. the firsst element will be a scalar - the first line of text. > I'm getting errors about: > > * Name "main::array2" used only once: possible typo at > ../h_test.pl line 14 (#1) you did only use it once. > * readline() on closed filehandle main::file at > ../h_test.pl line 13 (#2) > > * Your perl code sux ... go back to bash shell ... > line 22 (#2) - (*grin*) > > The errors go on to say that the filehandle i'm > trying to read got closed before the real fun > began. But how? I don't understand why the files > are closed. > > Anyhow ... anyone happen to have examples of > something like this around? > > -X