http://www.perlmonks.org?node_id=950741


in reply to Seeking help for using Tie : : File in my perl program for counting bases using Active Perl 5.10.1 Build 1007

There is no need at all to use Tie anything for this task. As with many many problems that are attacked using Perl the key is to use a hash. There are bunch of newbie foibles in your code that are worth trying to clean up sooner rather than later. I haven't described the issues explicitly, but have addressed them in the following code. Have a try using the code as a starting point, and please ask about anything you don't understand:

#!usr/bin/perl use strict; use warnings; if (! @ARGV) { print <<HELP; Usage: > basecount.pl <bases file> HELP exit; } open my $dnaIn, '<', $ARGV[0] or die "Can't open bases file $ARGV[0]: +$!\n"; my %counts; my @baseList = qw(A T G C); while (defined (my $line = <$dnaIn>)) { chomp $line; ++$counts{$_} for grep {/\S/} split '', $line; } my $bases; my $errors; $bases += $_ for @counts{@baseList}; $errors += $_ for map {$counts{$_}} grep {! /[ATGC]/} keys %counts; print "Total bases: $bases\n"; print join (', ', map {"$_: $counts{$_}"} @baseList), "\n"; print "Errors: $errors\n" if $errors;

Note that the code is untested and in any case I may not have understood what you are counting.

True laziness is hard work
  • Comment on Re: Seeking help for using Tie : : File in my perl program for counting bases using Active Perl 5.10.1 Build 1007
  • Download Code