You are repeating quite a bit of code there :) Try this (partially tested)-
my $file = 'file.txt';
# open for input is default and now our die tells us which
# file we were trying to open
open (DATA, $file) or die "Can't open $file $!\n";
# assuming you aren't using @DATA for something else later
# there is no need to slurp it all into memory now
while (<DATA>) {
next unless $. > 21;
chomp;
my @temp=();
foreach (split /,/){
s/"//g; # strip the quotes first if you want
s/\s+$//; # to remove trailing whitespace
push @temp, $_;
}
$compHash{$temp[0]} = $temp[1];
$aliasHash{$temp[2]} = $temp[3]; # you did mean $var4?
}
close (DATA);
I am sure it could be reduced further with a judicious use of map but I can't get my head around it this morning :(
Since you are parsing quoted CSV data you may want to look at Text::CSV which is a standard module or tilly's Text::xSV which is also available from CPAN.
-- my $chainsaw = 'Perl';
-
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.
|