By using arrays you get a more frugal memory usage and faster performance. You do trade off for readability when it comes time to use the data. Peronally, for maintainability I'll use a hash any day when I can give data a meaningful label rather than a cryptic number. I might even change the section around @line=split /,/ a bit to something along the lines of this:
Again, from the perspective maintainability I'd make two constructive comments for your suggestion. The first is the use of $_. I think this variable is confusing for newbies. Really when you think about it, a default variable is rather unique to Perl. As well, I feel it can be an accident waiting to happen. Sure, use them, especially if performance and crisp clean code is your objective. But, if you're not in a crunch I always do the following:[ defined earlier ] @fieldname = qw(Count Type Message); [ in the loop ] my @line=split /,/; foreach (my $i=0; $i<=$#line; $i++) { $record[$row]{$fieldname[$i]}=$line[$i]; }
Then it is really obvious that you have read a _line_ of DATA from your ol' data file.while ( $line=<DATA> ) { }
While we're talking about lines, here is my second constructive comment. You have chosen the variable @line to represent the splitting a line of data. A subtle notion, but I feel it is much clearer as to the meaning of data if you think about how it is to be accessed. After the split you are not accessing lines of data, you are accessing fields from a record or line. Calling it "@field" means that you're going to be referring to "$field[0]", "$field[1]", etc. which comes from a $line of <DATA>.
Not to say that there is anything _wrong_ with your code, but these would aid in long term maintainability. As I have seen, code does have a nasty habit of lasting longer than expected. Worst of all the quick 'n dirties often last the longest.
J
Update: fixed comparison on for loop - should be <=$#line, not <$#line. My mistake.
In reply to Re: Re: Help with parsing through a comma delimted file
by jlawrenc
in thread Help with parsing through a comma delimted file
by vonman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |