"automagic" that enables a little code to do a lot I find challengingto learn

"automagic" that enables a little code to do a lot I find challengingto learn

am 22.07.2011 02:39:31 von goldtech

Hi,

What i find challenging in perl is the "automagic" that enables a
little code to do a lot. I was looking at a book "Data Munging with
Perl" by David Cross. I've used perl in the past but I forget most of
it after a while and then relearn it when I need to use it again...In
any event (I wish I retained knowledge better) the very first script
in the book would be used on a file like, (assume tab delimited)

band1 song1 aaa 1999
band2 song2 bbb 1966
band3 song3 cccc 1988
band4 song4 dddd 1964
band5 song5 eee 1988

and the given code to get the number of CDs per year is:

my %years;

open FILE, "untitled" or die $!;

while () {
chomp;
my $year = (split /\t/)[3];
$years{$year}++;
}
foreach (sort keys %years) {
print "In $_, $years{$_} CDs were released.\n";
}

So "my $year = (split /\t/)[3];" knows that it's input parameter is a
line from it doesn't have to told. The [3] obviously says give
me the 4th element in the tab delimited line. The $_ var is well
documented. Again the minimal code approach is challenging for me. And
this can probably condensed even more ?!





--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/