Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Perl beginner here, needs a shove in the right direction.

by aaron_baugher (Curate)
on Jun 16, 2015 at 20:07 UTC ( #1130700=note: print w/replies, xml ) Need Help??


in reply to Perl beginner here, needs a shove in the right direction.

I wouldn't recommend slurping the files, since you want to look at individual lines. Just go through the files one by one, checking each file line-by-line. Since you're looking for design guidance rather than code, the logic would go something like this:

create a hash which has the keywords to watch for as its keys foreach file (probably using File::Find) do open file (error check) foreach line in file split the line into fields is the first field from this line in my hash of important keyw +ords? if yes, are there empty or dash fields in this line? if yes, report this file (and keyword and/or line number, optionally +) and move on to next file end of lines close file end of files

Take a look at File::Find; all the indented code above would be part of the callback you'd pass to File::Find's find() method.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.

Replies are listed 'Best First'.
Re^2: Perl beginner here, needs a shove in the right direction.
by rfromp (Novice) on Jun 16, 2015 at 21:31 UTC

    Thank you Aaron B, design is one of my weaknesses. You specifically say to "split the line into fields", however the lines are already delimited into fields by a forward slash, so is there something extra needed there?

    The line of the file I'm interested in looks something like this:

    DATA/-/data123/data456//data789/-/AZ

      I'm talking about using the split function to split the line into an array of fields, like this:

      my $line = 'DATA/-/data123/data456//data789/-/AZ'; my @fields = split '/', $line;

      that will put the fields in that array. Then you can check the first element of the array, $fields[0] , to see if it's in your hash of important keywords. If it is, you can grep the rest of the fields to see if any are the empty string or a dash. Here's an example with the sample line you gave:

      #!/usr/bin/env perl use 5.010; use strict; use warnings; my %keys = ('DATA' => 1); # setup a hash of keywords my $line = 'DATA/-/data123/data456//data789/-/AZ'; my @fields = split '/', $line; # split line into fields on a slash if( $keys{$fields[0]} ){ # is the first element in my hash of +keywords my $keyword = shift @fields; # remove the keyword from the fields +array if( grep { $_ eq '' or $_ eq '-' } @fields ){ # are any elements + empty or a dash? say "Line has problems, keyword $keyword"; } }

      Aaron B.
      Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2020-06-02 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (16 votes). Check out past polls.

    Notices?