Good day, I like so many, am new Perl. I am trying to search a file for a string, if found, pass it to an array. I don't think what I have done is the best way of doing things, but I learn by getting scripts to work and them improve on it.

I also think I may be missing a few concepts, or at lease the best practice.

When I run the script, I get....
968
968
3026
968
3026
809
968
3026
809
842
968
3026
809
842
25045
968
3026
809
842
25045
57344

I was expecting to get.....
968
3026
809
842
57344

#!C:/Perl/bin/perl.exe
use strict;
use warnings;

### Script Variables
my $FIND = "(C|c)ontent-(L|l)ength:" ;
my $FILES ;                             # files
my @FILELIST ;                          # array for files
my $IP_DIR="E:/IP_DIR" ;                # Input Directory
my $LINES ;

### Verify Input  Directory
opendir(IP_DIR, $IP_DIR) or die ("$IP_DIR does not exist!\n") ;

### Send FILES to array
opendir DIR, $IP_DIR ;
@FILELIST = readdir DIR ;

# Remove the '.' and the '..' from array
delete @FILELIST[0,1] ;
#print "@FILELIST" ;

### Open FILES in FILELIST
foreach $FILES (@FILELIST) {
#        chomp $FILES ;
        open FILE, "<$IP_DIR\/$FILES";
        my @FILE = <FILE> ;

#print @FILE ;
my @START="" ;
             for (@FILE) {
             if ($_ =~ /$FIND/) {
             push(@START,$_) ;
             print @START ;
                }
             }
}

Recommended Answers

All 4 Replies

Perhaps you should post in the Perl forum. This is the Java forum.

Some input data would also be nice.

Sorry for posting in wrong forum. Thought I was in Perl, glad I had no money on it. Below is a small sample of the text.

--- Start Sample Text ---
ETag: "3cbfc7-3c8-482dba84"
Accept-Ranges: bytes
Content-Length: 968
Connection: close
Content-Type: image/gif
X-Pad: avoid browser bug
GIF89a F\0±¢D`Ô˜‘A~dHÒ¡EŠ#Py²åE”@ÆÄ(³&Í›3sÚÔ‰s§O4ªqH£
*Mê1¡§NY¦ÄAÕˆ@fª5a¯
—"EI¶*dz/O6\Ë`@BÈe
f-¬€ETag: "3cbfc7-3c8-482dba84"
Accept-Ranges: bytes
Content-Length: 3026
Connection: close
Content-Type: image/gif
X-Pad: avoid browser bug
GIF89a F\0±¢D`Ô˜‘A~dHÒ¡EŠ#Py²åE”@ÆÄ(³&Í›3sÚÔ‰s§O4ªqH£
*Mê1¡§NY¦ÄAÕˆ@fª5a¯
—"EI¶*dz/O6\Ë`@BÈe
f-¬€
--- END Sample Text ---

With alittle work over the weekend, the following works.

#!C:/Perl/bin/perl.exe
use strict;
use warnings;
#use File::Copy;
use File::Find;

### Script Variables
my $FIND = "(C|c)ontent-(L|l)ength: " ;
my $FILES ;                             # files
my @FILELIST ;                          # array for files
my $IP_DIR="E:/IP_DIR" ;                # Input Directory
my $OP_DIR="E:/OP_DIR" ;                # Output Directory
my @STARTSTOP = $' ;                    # Start & Stop points
          
### Verify Input & Output Directory
opendir(IP_DIR, $IP_DIR) or die ("$IP_DIR does not exist!\n") ;
opendir(OP_DIR, $OP_DIR) or die ("$OP_DIR does not exist!\n") ;

### Send FILES to array
opendir DIR, $IP_DIR ;
@FILELIST = readdir DIR ;

# Remove the '.' and the '..' from array
delete @FILELIST[0,1,] ;
#print "@FILELIST \n";

### Open FILES in FILELIST
foreach $FILES (@FILELIST) {
     open FILE, "<$IP_DIR\/$FILES";
     my @FILE = <FILE> ;

### print @FILE ;
my $NUM ;
my $TEST ;
my @TEST ;
foreach (@FILE)
     {
          if ($_ =~ /$FIND/)
          {
          foreach $NUM ($')
               {
               push(@TEST, $NUM) ;
               }
          }
     }
          print $TEST[0] ;
          print $TEST[5] ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.