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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|