Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Reading through a file and checking for a specific string

by 2teez (Priest)
on Aug 19, 2013 at 20:16 UTC ( #1050081=note: print w/ replies, xml ) Need Help??


in reply to Reading through a file and checking for a specific string

Scanning through your codes, there are several stuff, that is wrong.
Please,

use warnings; use strict;
to start with, then you will discovery that several of your codes didn't end with a ;.
You said you are using a glob function, but their is non in your code, so you only have the string "/export/home/date_file*.txt", in your array variable "@files".
Update:
I have array1 - @types that has these 2 letter strings(which I am supposed to check for in each file in each line, This variable is found in each line at 41st character.) and array2 - @counts where I am trying to store count if that match is found.
Instead of using two arrays, why don't you use a HASH, with the type of strings you would be looking for as the keys and initialized them to zero like so:
my %type; @type{qw(AB AC AD AE FG)} = (0) x 5;
##then you have something like this: $VAR1 = { 'AC' => 0, 'AE' => 0, 'FG' => 0, 'AB' => 0, 'AD' => 0 };
then later you can do... *
while(...){ ... $type{$_}++; ## increasing the counting as you see needed string ... }
*NOTE:
I don't except the pseudo-code to work, since the OP didn't show any dataset.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me


Comment on Re: Reading through a file and checking for a specific string
Select or Download Code
Re^2: Reading through a file and checking for a specific string
by vihar (Acolyte) on Aug 19, 2013 at 20:20 UTC
    Hi, I just updated it in my code. There were a few lines I added manually when I was writing this question. I forgot to put semi colons and glob function but I did that now. Thanks

      Yes, you have, but there are still some there.
      You still have a single quote without it's corresponding part here

      @files = glob ('/export/home/date_file*); ^ ^<-- not there
      Then instead of these
       $counts[$i] ...
      $files[$i]..
      $counts[$i]++..
      you are writing  @counts[$i] ...
      @files[$i]..
      @counts[$i]++.. Which in this case is not correct.

      If I may give you a head up ( it might not be the best suited for you but it might point you in a right direction ).
      I have a few text file in a directory, and am reading each file to see how many times some certain words were used like this:
      use warnings; use strict; use Data::Dumper; my %type; @type{qw(is a an at)} = (0) x 4; for my $file ( glob "*.txt" ) { open my $fh, '<', $file or die $!; while (<$fh>) { for (split) { $type{$_}++ if exists $type{$_}; } } } print Dumper \%type; my $total = 0; $total += $type{$_} for keys %type; print $total, $/;

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1050081]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2014-08-01 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (33 votes), past polls