Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something is wrong with my Loop. 2

Status
Not open for further replies.

9thHouse

MIS
Aug 26, 2010
3
CH
Hi all,
I have been given a task to search for strings in a file that is encoded. I need to display the file name only when all the 3 strings which i provide are present in the file name.

i first try to get a list of files according from the directory according to the value passed in argument 1. For each file in the list i then try to search for a strings in the file
Code:
use Time::localtime;
use POSIX qw/strftime/;
use File::Find;
use File::stat;
use IO::Handle;
use File::copy;
use Unicode::String ;
use Cwd;
use Encode;
use PerlIO::encoding;

Unicode::String->stringify_as( 'utf8' );
sub trim($);


$dir = "C:\\Temp\\t\\";


($File_pattern,$search_pattern,$search_pattern2,$search_pattern3) = @ARGV;
$SRC_FLG1=0;
$SRC_FLG2=0;
$SRC_FLG3=0;
#print $search_pattern2;
print "Searching $search_pattern,$search_pattern2,$search_pattern3 in $dir......\n";


$search_pattern = trim($search_pattern);

$search_pattern2= trim($search_pattern2);

$search_pattern3= trim($search_pattern3);

sub trim($)
{
  my $string = shift;
  $string =~ s/^\s+//;
  $string =~ s/\s+$//;
  return $string;
}




opendir ( DIR, $dir ) || die "Error in opening dir $dirname\n";
find (\&Search, "$dir");

sub Search {
if ((/\.msg$/) && (/$File_pattern/) )
               {
                print $_;

                foreach $x ($_) {
                my $filename = $x;
                print "\n--------------------------------------------------\n";
                open(my $in, '<:raw', $filename) || die "Couldn't open file: $!";
                 my $text = do { local $/; <$in> };
                close $in;
                my $content = decode('UTF-16LE', $text);

                      foreach $line ($content)
                                              {  print "search $search_pattern in $content";
                                                 if ($line =~ (m/$search_pattern/gi))
                                                      {
                                                        print "\nYes1";
                                                        $SRC_FLG1=1;
                                                      }

                                                                        print "search $search_pattern2 in $content";
                                                  if ($line =~ (m/$search_pattern2/gi))
                                                        {
                                                         print "\nYes2";
                                                        }
                                               print "search $search_pattern3 in $content";
                                                                        if ($line =~ (m/$search_pattern3/gi))
                                                         {
                                                          print "\nYes3";
                                                          $SRC_FLG3=1;
                                                         }

                                  if ($SRC_FLG1==1 && $SRC_FLG2==1 && $SRC_FLG3==1)
                                        {
                                         print "File found : $filename\n";
                                        }

                      print $SRC_FLG1,$SRC_FLG2,$SRC_FLG3;
                      $SRC_FLG1=0;
                      $SRC_FLG2=0;
                      $SRC_FLG3=0;

                                            }


                                }

                  }








print "\n--------------------------------------------------\n";

closedir(DIR);
chdir("..");

- The strange thing is that it never goes into the loop
Code:
if ($line =~ (m/$search_pattern2/gi))

if i replace loop for search_pattern3 and put it before search_pattern2 then the code does not go into search_pattern3. So it is always the second loop. i have tried to enter same values for string1,string2,string3 but still it does not matches.
Could someone help ?
 
And if statement is not a "loop", only things like for and while statements are loops.

Where is the closing } of your Search subroutine?

Why are you opening a DIR handle when it is never actually used?

Why do you have a foreach $x ($_)? If you check perldoc File::Find you'll see that the &wanted function (in this case your Search subroutine) is called once for each individual file anyway, so there's no need to introduce another loop inside it.

Sorry to answer your question with a whole bunch of questions, but I think the code needs tidying up a bit before we can isolate the cause of your problem. Also I'd recommend turning on warnings (use warnings; or #!/usr/bin/perl -w) if you haven't already.

Annihilannic.
 
I will simplify my code

Code:
open(my $in, '<:raw', $filename) || die "Couldn't openfile:$!";
  my $text = do { local $/; <$in> };
  close $in;
  my $content = decode('UTF-16LE', $text); 
                      #print $content;
                      if ($content =~ (m/$search_pattern/gi)) { print "\nYes1.0";}
                      if ($content =~ (m/$search_pattern/gi)) { print "\nYes1.1";}

When i use the above code, eventhough i am searching for the same pattern in the file in both the loops still i only get output Yes1.0 , the second loop never works, i added few more if conditions and i see that only the alternate loop works, not sure if this is due to the way i decode my file, Apologies - i am still quite new to perl.
 
when you use m//g, a position is set at the offset within the string where the first match is found. The next match starts searching where the last one left off. Just remove the g's

see:
perldoc perlop
perldoc -f pos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top