Question

Why does the following script take so many cycles to complete?

The document it is sifting through is 20590 lines long and each line consists of the following, or a variation thereof; "10160354001 ALGIERS ALGERIA 36.70 3.60 290"

I am trying to make a database to match the first number to the last 3.

The actual program is however taking several seconds per line.

Script:

#! /usr/bin/perl

$path = shift(@ARGV);

open COORDS, $path or die "Couldn't open file\n";

my %coords = {};

foreach $line (<COORDS>){
    $stat = substr $line,0,11;
    @coords = grep(($_!=undef),split(/ /,substr $line,42));
    $coords[$stat]=@coords;
}

print $coords['10160354001'];

close(COORDS);
Was it helpful?

Solution

$coords['10160354001'] = ... is an assignment to an array element, and a large one at that. This statement will cause Perl to allocate an array with room for at least 10160354002 elements.

You meant to use a hash:

$coords{$stat} = "@coords";
...
print $coords{'10160354001'};

use strict and use warnings would have alerted you to this and other problems with your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top