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

2011-07-22 Thread 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    1988
band4   song4     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 (FILE) {
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 FILE 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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




testing for a file type

2007-12-18 Thread goldtech
Hi,

If I have:

...
foreach (@ARGV) {
print do something only to .mdb files;
}

I could use File::Basename's fileparse and test for the file extension
and put a big if statement around or in the foreach loop. So if a user
puts a non .mdb file argument on the cmd line it won't process and
prints a usage.

But I suspect in perl theres a more compact way of doing that?

Thanks.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/