in reply to Re: Search Script
in thread Search Script

Hello David, Thank you very much for your ideas. The next time I post anything I will make sure to comment my code, I apologize for that. I changed the structure of the code, do you think I could still use the
while( my $hintline = <HINTS> ){ my ($first,$last) = split(/\ == /,$hints); my $results = `strings $filename | grep -i '$first'`; next unless length $results; printf "Found: %s\n", $last;
for the search. What is the "my" used for? Sorry I'm new to Perl.

Replies are listed 'Best First'.
Re^3: Search Script
by eibwen (Friar) on May 04, 2005 at 17:56 UTC

    Consult perlfunc, which contains the following for my:

    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.

      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?
Re^3: Search Script
by davidrw (Prior) on May 04, 2005 at 18:32 UTC
    The "my" is used to declare variables (see eibwen's post).

    You should also include this:
    use strict; use warnings;
    at the top of your scripts -- what these will do is enforce (or warn) about certain things that will save you lots of trouble in the long-run, and point out compiling problems. Check out the manpages for each ("man strict", "man warnings") for more details. Something it is especially useful for is enforcing variable declarations:
    This will run, but NOT do what you want:
    $myvariable = 3; print "Pi is exactly $myvaraible";
    This code, however, will give a very informative error message if you try to run it:
    use strict; my $myvariable = 3; print "Pi is exactly $myvaraible";