
SMcG
New User
Sep 4, 2013, 3:23 AM
Post #1 of 7
(2557 views)
|
Modifying a perl script
|
Can't Post
|
|
Hi there, I'm kinda brand new to perl (well programming in general), and have been presented with a perl script (Id_script3.pl). The script requires a modification, in which 'SpeciesId3.txt' will be used instead of the 'SpeciesId.txt' which is currently used by the script. There is a slight difference between the two files, so a slight modification would need to be made to the script for it to function; the difference being that SpeciesId3.txt contains no letters (A =, B =, C =) and simply a (much) longer list of values as compared to the original 'SpeciesId.txt'. I've tried a couple of attempted "work-arounds" but am yet to discover one that works. Many Thanks, Stephen. # main sub { # closure # keep %species local to sub-routine but only init it once my %species; sub _init { open my $in, '<', 'SpeciesId.txt' or die "could not open SpeciesId.txt: $!"; my $spec; while (<$in>) { chomp; next if /^\s*$/; # skip blank lines if (m{^([A-Z])\s*=\s*(\d+(?:\.\d)?)(?:\s+AND\s+(\d+(?:\.\d)?))?$}) { # handle letter = lines $species{$spec}{$1} = [$2]; push @{$species{$spec}{$1}}, $3 if $3; } else { # handle species name lines $spec = $_; $len = length($spec) if (length($spec) > $len); } } close $in; } sub analyze { my ($masses) = @_; _init() unless %species; my %data; # loop over species entries SPEC: foreach my $spec (keys %species) { # loop over each letter of a species LTR: foreach my $ltr (keys %{$species{$spec}}) { # loop over each mass for a letter foreach my $mass (@{$species{$spec}{$ltr}}) { # skip to next letter if it is not found next LTR unless exists($masses->{$mass}); } # if we get here, all mass values were found for the species/letter $data{$spec}{cnt}++; } }
|