in reply to Re^2: Search Script
in thread Search Script

Consult perlfunc, which contains the following for my:

       my EXPR
       my TYPE EXPR
       my EXPR : ATTRS
       my TYPE EXPR : ATTRS
               A "my" declares the listed variables to be local (lexically) to the enclosing block,
               file, or "eval".  If more than one value is listed, the list must be placed in paren-
               theses.

               The exact semantics and interface of TYPE and ATTRS are still evolving.  TYPE is cur-
               rently bound to the use of "fields" pragma, and attributes are handled using the
               "attributes" pragma, or starting from Perl 5.8.0 also via the "Attribute::Handlers"
               module.  See "Private Variables via my()" in perlsub for details, and fields,
               attributes, and Attribute::Handlers.

While I still don't understand why you return $last if $first is found, I think you may be looking for a hash:

#!/usr/bin/perl -w use strict; my %hints; open FILE, 'hints'; while (<FILE>) { chomp; my @hint = split(/\s*==\s*/); $hints{$hint[0]} = $hint[1]; } close FILE; my $filename = 'file'; open FILE, $filename; binmode FILE; { local $/; my $file = <FILE>; print map { $file =~ /$_/ ? $hints{$_} : () } keys %hints; } close FILE;

Granted the above code is likely slower than using `strings | grep`, but it illustrates that this can be done in pure perl.

Replies are listed 'Best First'.
Re^4: Search Script
by arebc (Initiate) on May 04, 2005 at 18:54 UTC
    The $first and $last names might be a littel misleading. $first is a string that might be found in a file and $last is the category that the string belongs to. I have updated my code but its still not done yet
    print"\n"; print "Please enter file name and path:"; chomp($filename = <STDIN>); #Gets File to Search print "File Name: $filename"; #Prints the File Name print "\n"; #Skips Line #open DAFILE, "$filename"; #Opens the file to Search open(testfile,"Hints.txt") or die "Can't find file\n"; #Opens Hints.tx +t $i=0; #Var array counter @daarray = (); #Var Array while(<testfile>){ #While Hints.txt file is open $daarray[$i] = $_; #Loads each line of Hints.txt into array $i++; # array's index } close(testfile); #Close Hints.txt for($j=0; $j<$i; $j++) #for each item in array { ($first,$last) = split(/\ == /,$daarray[$j]); #Splits hints line $results = `strings $filename | grep -i '$first'`; #String Search next unless length $results; printf "Found: %s\n", $last; } print "Search Complete\n"; #Print #close DAFILE; #close DAFILE
    Sorry, I'm a little new to Perl and I didn't understand all you code. How could I rewrite the script so its all Perl?