There is the defined and exists functions.
snip...
my @row = split(/;/, $array[$_]);
if ( defined @row ) {
$hash{$_} = \@row;
} else {
print "Empty line at: $_\n";
}
snip...
Alternately the defined could be exists.
Some comments:
1) Since you are not returning slices from your split, there is no need for the parentheses surrounding it.
2) Within the split itself, are you afraid that a semicolon will be interpreted? If so dont be, within the regular expression it is treated as a literal character. If on the other hand you are matching a literal backslash, you need to change the regex to /\\;/, as the back slash is a metacharacter.
3) Since you appear to be using the line number as a hash index, you could try something along the lines of the following which uses $. This var keeps track of what line you are on in the file in question (Note it begins counting at 1 not 0 like normal). This trick will reduce the memory you use while running this chunk of code
open(IN, "$file") || die "Cant open $file\nReason: $!\n";
while (<IN>) {
chomp();
my @row = split(/;/);
if ( defined @row ) {
$hash{$.} = \@row;
} else {
print "Empty line in $file at line: $.\n";
}
}
4) Since I my'd @row inside of the while loop, every iteration of the loop @row will be emptied by perl itself, which can avoid the redundant line @row = (); at the end of your loop.
Just some pointers :)
/* And the Creator, against his better judgement, wrote man.c */
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.