CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Modifying a perl script


New User

Sep 4, 2013, 3:23 AM

Post #1 of 7 (2608 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 (
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,


# 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>) {
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
foreach my $spec (keys %species) {
# loop over each letter of a species
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

Attachments: (4.60 KB)
  SpeciesId.txt (3.85 KB)
  SpeciesId3.txt (27.8 KB)


Sep 4, 2013, 6:08 AM

Post #2 of 7 (2602 views)
Re: [SMcG] Modifying a perl script [In reply to] Can't Post

Your existing script very much depends on the 'letters'. You need a scheme to infer them from the new data. If this is not possible, I doubt that the existing script is going to be much help at all in processing the new data. Please try to describe both the old and new data in terms that programmers (not biologists) can understand.
Good Luck,

New User

Sep 4, 2013, 6:47 AM

Post #3 of 7 (2595 views)
Re: [BillKSmith] Modifying a perl script [In reply to] Can't Post

Hi Bill,

I've attached an example workthrough (example_workthrough.txt) along with an example set of data to try to clarify the scripts function along with its usage of data.

Thanks again,

Attachments: example_workthrough.txt (4.80 KB)
  20130730_p12_A7.csv (7.11 KB)
  SpeciesId3.txt (27.8 KB) (4.61 KB)
  CSVfile_structure.jpg (168 KB)

New User

Sep 4, 2013, 7:13 AM

Post #4 of 7 (2592 views)
Re: [SMcG] Modifying a perl script [In reply to] Can't Post

Basically, for each CSV results file, I want to take the mass lists and see which animal (from SpeciesId3.txt) each CSV file best aligns (most matches) to, with match scores and percentage matches.

Veteran / Moderator

Sep 4, 2013, 10:34 AM

Post #5 of 7 (2586 views)
Re: [SMcG] Modifying a perl script [In reply to] Can't Post

I do not think that using a closure is the easiest way to start learning Perl for a beginner.

New User

Sep 5, 2013, 7:47 AM

Post #6 of 7 (2574 views)
Re: [Laurent_R] Modifying a perl script [In reply to] Can't Post

Thanks Laurent R,

So how would you suggest I go about this then?



Veteran / Moderator

Sep 5, 2013, 12:01 PM

Post #7 of 7 (2570 views)
Re: [SMcG] Modifying a perl script [In reply to] Can't Post

Well, I don't really have the time now to look into it and look at your various file to really understand the details of what you want to do.

Two points.

1. As mentioned by others, your current script relies heavily on the current structure of your file, most of it will be unusable with the new data.

2. Closures are a nice features of Perl, pretty useful, but they are a somewhat advanced topic. And as far as I can say from reading quickly your program, it is not extremely useful in the case in point and it might give headaches to a beginner. I would simply make %species a global variable in this case.

Removing the closure feature is very easy, but changing the program to adapt to the new data format is slightly more complicated, because you need first to understand the format and figure out what to extract. I don't have time for that right now (probably not before the weekend).


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives