#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Basic search script for a file


    Hello

    I am trying to learn perl and I can't find way to search a small text file. I want to search the file for a spesific word that is input from the user. If the word is found to tell him that is in the file and if not in the file to write into the file.

    I am trying to put the file contend in an array and search the array(failed). I can't find anything anywhere so I am turning to you for help. I read that there is ~~ to match in array but this is if you are using higher version than 5.8.8 and I am on 5.8.8. Got any tips how to do this basic task ? Please if are willing to provide a solution can you write up a little explanation to it?

    Thank you for reading


    P.S. I am very shy to show my work so far it is that bad. IF you want to see where am I will post it
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Don't be shy.

    If you show your code, we will be able to help you to correct your code. Withoŗut your code, I could give you a solution that would be so different from what you have in your code that it might be unusable for you without rewriting everything. In addition, showing your code might expose you to criticks, but it is a wonderful opportunity for you to progress.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    My work so far


    Code:
    #!/usr/bin/perl -w
    
    open(FH,'>dictionary.txt') or die "Can't create dictionary.txt: $!";
    
    print FH "Perl - Practical Extraction Report Language\n";
    close FH;
    
    print "enter a word\n";
    $word = <>;
    
    if (grep{/$word/} <dictionary.txt>){
       print "word found\n";
    }
    else{
       print "word not found\n";
    }
    I know was thinking is it possible to put the file(dictionary.txt) data in an array and then use =~ m/[$word]/ ?? or something like it ?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Hi,

    this code:

    Perl Code:
    if (grep{/$word/} <dictionary.txt>){
       print "word found\n";
    }


    is wrong, because the Perl grep function has very little to do with the Unix grep function. In Perl,the grep function is NOT looking for a pattern in a file.

    In Perl, the grep function looks for a pattern in the elements of an array or a list and returns the elements of the array that match the pattern.

    For example:

    Code:
    my @matching_elements = grep { /oo/} qw /foo bar fool baz perl gem boom grutch tool what a silly list /;
    # now, @matching elements contains foo, fool, boom, tool
    But you can't use the Perl grep function for exploring a file the way you would do it with the Unix grep function.

    If you want to look for a word in a file, you'll have to read each line of the file, for example like this:

    Code:
    my $searched_pattern = qr/practical/;
    open my $DICT, "<" "dictionary.txt" or die "cannot open file $!";
    while (my $line = <$DICT>) {
         print "found match on line $. : $line \n" if $line =~/$searched_pattern/; # will print your line if "practical" is found
    }
    To tell the full truth, now that I have shown (and, I hope that you basically understand) the two examples above, I should add that the grep function could actually be used if you first "slurp" the file into an array (which you should not do if there is any chance that your input file might become very large). Once your file is in an array, you can use grep as explained above to work on that array. That would be something like this:

    Code:
    my $searched_pattern = qr/practical/;
    open my $DICT, "<" "dictionary.txt" or die "cannot open file $!";
    my @lines = <$DICT>); # slurps the file lines into the @lines array
    my @matches = grep { /$searched_pattern/ } @lines;
    # now, @matches contains the lines matching your pattern 
    print "$_\n" foreach @matches;

    Comments on this post

    • ishnid agrees
    Last edited by Laurent_R; December 4th, 2012 at 05:25 PM.
  8. #5
  9. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,251
    Rep Power
    1810
    Just want to add that when asking a question about searching or parsing a file, it is helpful to provide at least a sample of that file.

    Often makes a difference in the way we would approach the problem.

IMN logo majestic logo threadwatch logo seochat tools logo