FILEHANDLE Questions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sevla
    New Member
    • May 2009
    • 16

    FILEHANDLE Questions

    hello

    im novice perl programmer, and i need a little help about manipulating files, as text.txt for example,

    i need seek some specific "word" or information on the txt file or any external file and display an error log if the word/info was not found, such as creating a log file reporting with details

    and i have not known the code to do this yet,

    Thanks for patience
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    If u need to search for a pattern and display an error then why cant u use grep from a shell script and do it?

    raghu

    Comment

    • Icecrack
      Recognized Expert New Member
      • Sep 2008
      • 174

      #3
      what have you tried so far? lets see some code.

      Comment

      • Sevla
        New Member
        • May 2009
        • 16

        #4
        thats a simple example, im trying to make a searching by a list name, looking for a specific word or name,

        Code:
        #!C:/perl/bin/perl.exe
        
        @list_name = (carlos,jonas,bruno,jonas,milles,cassio,otario,
        felipe,loko,anta,paulo,roberto,jose,pablo,srv,zeraldo,eduardo,
        caio,kaua,diego,fabio,pedro,helbert,geraldo,jonathan,linus,
        theo,ken,monstro,otaciolio,tonhao,beto,dartanhan,joe,jimi),
        
        print "Do you want search for a name?\n";
        $choose=<STDIN>; chomp $choose;
        if($choose =~ /[Y,y]es|ok/) {
        print " which one\?\n"};
        $choose=<STDIN>; chomp $choose;
        foreach(grep(/@_/,@list_name)) {
        print "results:$_\n";
        }
        thanks for patience
        Last edited by numberwhun; May 22 '09, 10:18 PM. Reason: PLEASE use code tags!

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          That is a logical mistake. The pattern for grep should be $choose:
          Code:
          foreach(grep(/$choose/,@list_name)) {
          print "results:$_\n";
          }

          P.S: Please use code tags while posting code in future

          Comment

          • Sevla
            New Member
            • May 2009
            • 16

            #6
            my falt,

            thanks a lot,

            i just started programming these last few days,

            Comment

            • Sevla
              New Member
              • May 2009
              • 16

              #7
              the last question about this topic...

              with external files as text.txt i can use this code?

              by replace the @list_name for open ( $file "c:....../file.txt")|| die "failed to open text.txt $!\n"; ??

              thanks for your kind patience

              Comment

              • Sevla
                New Member
                • May 2009
                • 16

                #8
                can someone help me with code to get a specific word, from a text.txt

                here is the code, i cant find where is mistake
                Code:
                #!C:/perl/bin/perl.exe
                open (my $read, "c:/perl/notice.txt" )|| die "failed to open notice.txt $!\n";
                while(<$read>) {
                print "$_";
                }
                close $read;
                
                print "search a word\?\n";
                $choose=<STDIN>; chomp $choose;
                if($choose =~ /[yes|ok]/) {
                print "digit a word\n"};
                $choose=<STDIN>; chomp $choose;
                foreach(grep(/$choose/,my $read)) { 
                print "result:my $read\n"; 
                }
                obs: this is a veery simple code, i didnt complete the conditions as you can see , my focus is to make the perl get a word from the text.txt and display it on prompt as i asked
                Last edited by numberwhun; May 22 '09, 10:20 PM. Reason: PLEASE use code tags!

                Comment

                • numberwhun
                  Recognized Expert Moderator Specialist
                  • May 2007
                  • 3467

                  #9
                  I think that the first thing you really need to learn is to include the following two pragmas after your shebang line:

                  Code:
                  use strict;
                  use warnings;
                  They will help alleviate all of the typical coding mistakes that are made, especially by beginners. I know a lot of people who won't look at your code unless you do use them, so it is highly advised.

                  Also, it was mentioned to you earlier in this thread and I am now saying it. Please use code tags. They are required around any code you include in the forums and if you don't add them, then we have to clean up behind you.

                  Thanks!

                  Jeff

                  Comment

                  • Sevla
                    New Member
                    • May 2009
                    • 16

                    #10
                    hello guys, i solved the searching topic, i just need a help about how make a error message if your searched word were not found

                    Code:
                    #!C:/perl/bin/perl.exe
                    #filehandle, opening
                    start:
                    open FILE, "c:/perl/noticia.txt" || die "failed to open notice.txt $!\n";
                    @read=<FILE>;   # read entire file into array
                    close FILE;
                    chomp @read;
                    
                    #searching for a key word inside the txt file
                    
                    print "search a word\?\n";
                    $choose=<STDIN>; chomp $choose;
                    if($choose =~ /yes|ok/) {
                            print "enter a word\n";
                            $choose=<STDIN>; chomp $choose;
                            foreach(grep(/$choose/,@read)) {
                                    print "result: $_\n";
                            }
                    }
                    else{
                    	print "invalid\n";
                    	goto start;
                    
                    }
                    Last edited by eWish; May 26 '09, 07:17 PM. Reason: Please use the [code][/code] tags.

                    Comment

                    • nithinpes
                      Recognized Expert Contributor
                      • Dec 2007
                      • 410

                      #11
                      You can save the result of grep into an array and then run the test.
                      Code:
                      my @res = grep(/$choose/,@read);
                      print "STRING NOT FOUND!!!" unless(@res); 
                      foreach(@res) {
                      print "result: $_\n";
                      }

                      Comment

                      • Sevla
                        New Member
                        • May 2009
                        • 16

                        #12
                        great man, thanks a lot

                        Comment

                        Working...