Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3281874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:51:52+00:00 2026-05-17T19:51:52+00:00

I have a Perl script, which parses datafile and writes 5 output files filled

  • 0

I have a Perl script, which parses datafile and writes 5 output files filled with 1100 x 1300 grid. The script works, but in my opinion, it’s clumsy and probably non-efficient. The script is also inherited code, which I have modified a little to make it more readable. Still, it’s a mess.

At the moment, the script reads the datafile(~4Mb) and puts it into array. Then it loops through array parsing its content and pushing values to another array and finally printing them to file in another for loop. If value is not found for certain point, then it prints 9999. Zero is an acceptable value.

The datafile has 5 different parameters and each of them is written to its own file.

Example of data:

data for the param: 2
5559
// (x,y) count values
280 40 3  0 0 0 
280 41 4  0 0 0 0 
280 42 5  0 0 0 0 0 
281 43 4  0 0 10 10 
281 44 4  0 0 10 10 
281 45 4  0 0 0 10 
281 46 4  0 0 10 0 
281 47 4  0 0 10 0 
281 48 3  10 10 0 
281 49 2  0 0 
41 50 3  0 0 0 
45 50 3  0 0 0 
280 50 2  0 0 
40 51 8  0 0 0 0 0 0 0 0
...

data for the param: 3
3356
// (x,y) count values

5559 is the number of data lines to current parameter. Data line goes: x, y, number of consecutive x-values for that particular point and finally the values.
There is an empty line between parameters.

As I said earlier, the script works, but I feel like this could be done so much easier and more efficiently. I just don’t know how. So here’s a chance for self-improvement.

What would be better approach to this problem, than a complicated combo of arrays and for-loops?

EDIT:

Should’ve been more clear on this, sorry.

Output is 1100 x 1300 grid filled with values read from data file. Each parameter is written to different file. More than one values on the data line means, that line has data for x(+n), y points.

UPDATE:

I tested the solution and to my surprise it was slower than original script (~3 seconds). However, the script is ~50% smaller, which makes it lots easier to actually understand what the script does. In this case that’s more important than a 3-second speed gain.

Here some of the code from the older script. Hope you’ll get the basic idea from it. Why is it faster?

 for my $i (0..$#indata) { # Data file is read to @indata
 ...
   if($indata[$i] =~ /^data for the param:/) { 
     push @block, $i;  #  data borders aka. lines, where block starts and ends
   }
 ...
 }
  # Then handle the data blocks
 for my $k (0..4) {  # 5 parameters
 ...
   if( $k eq '4') {  # Last parameter
     $enddata = $#indata;
   }
   else {
     $enddata = $block[$k+1];
   }
    ...
   for my $p ($block[$k]..$enddata) { # from current block to next block 
    ...
   # Fill data array
    for(my $m=0 ; $m<$n ; $m++){
    $data[$x][$y] = $values[$m];
     }

   }
   print2file();

 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:51:53+00:00Added an answer on May 17, 2026 at 7:51 pm

    The following will fill in a sparse array in a hash. When printing, print 9999 for cells with undefined values. I changed the code to build each row as a string to reduce the memory footprint.

    #!/usr/bin/perl
    
    use strict; use warnings;
    use YAML;
    
    use constant GRID_X => 1100 - 1;
    use constant GRID_Y => 1300 - 1;
    
    while (my $data = <DATA> ) {
        if ( $data =~ /^data for the param: (\d)/ ) {
            process_param($1, \*DATA);
        }
    }
    
    sub process_param {
        my ($param, $fh) = @_;
        my $lines_to_read = <$fh>;
        my $lines_read = 0;
    
        $lines_to_read += 0;
    
        my %data;
    
        while ( my $data = <$fh> ) {
            next if $data =~ m{^//};
            last unless $data =~ /\S/;
            $lines_read += 1;
    
            my ($x, $y, $n, @vals) = split ' ', $data;
    
            for my $i ( 0 .. ($n - 1) ) {
                $data{$x + $i}{$y} = 0 + $vals[$i];
            }
        }
        if ( $lines_read != $lines_to_read ) {
            warn "read $lines_read lines, expected $lines_to_read\n";
        }
    
        # this is where you would open a $param specific output file
        # and write out the full matrix, instead of printing to STDOUT
        # as I have done. As an improvement, you should probably factor
        # this out to another sub.
    
        for my $x (0 .. GRID_X) {
            my $row;
            for my $y (0 .. GRID_Y) {
                my $v = 9999;
                if ( exists($data{$x})
                        and exists($data{$x}{$y})
                        and defined($data{$x}{$y}) ) {
                    $v = $data{$x}{$y};
                }
                $row .= "$v\t";
            }
            $row =~ s/\t\z/\n/;
            print $row;
        }
    
        return;
    }
    
    
    __DATA__
    data for the param: 2
    5559
    // (x,y) count values
    280 40 3  0 0 0 
    280 41 4  0 0 0 0 
    280 42 5  0 0 0 0 0 
    281 43 4  0 0 10 10 
    281 44 4  0 0 10 10 
    281 45 4  0 0 0 10 
    281 46 4  0 0 10 0 
    281 47 4  0 0 10 0 
    281 48 3  10 10 0 
    281 49 2  0 0 
    41 50 3  0 0 0 
    45 50 3  0 0 0 
    280 50 2  0 0 
    40 51 8  0 0 0 0 0 0 0 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script which uses a not-so-common module, and I want it
The situation I am in is that I have written a perl script which
I have to display some print statements (which I get while running a Perl
I am writing a Perl script which does this : Reads the values of
I have a locked down kiosk terminal server. This terminal server has a perl
I have two scripts in which I'm experimenting with CSV_XS. In the first, I
I am using CGI.pm version 3.10 for file upload using Perl. I have a
I need to make running multiple Perl scripts as easy as possible, and make
I've tried using system() with fork(), tried exec(), and am still not getting what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.