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

  • Home
  • SEARCH
  • 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 6161991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:34:30+00:00 2026-05-23T21:34:30+00:00

The program that I am writing needs to do this: read every line of

  • 0

The program that I am writing needs to do this:

  • read every line of a file
  • if the line contains an ordered pair (x,y) store the ordered pair
  • before the next ordered pair, there will be a line of the file that starts with “Results”
    • store the ordered pair at the end of that line as a “value” and “error”
  • print out the corresponding x, y, value, error in CSV format
  • read the next (x,y) value and so on, the (x,y) lines and (value,error) lines will alternate in the file

This is not a homework assignment. As you can see, I already have code that works down to 17 lines. I’m wondering if I can accomplish this task with any fewer lines or cleaner code, while maintaining at least the level of readability that this version has, and maintaining Perl style (such as the line break between includes and the first executable line).

The line that I am least thrilled with is

if (defined($x) && defined($y) && defined($val) && defined($err))

Is there a better way to do an an assertion to take care of the alternating data in the file? If I don’t use the defined() function, the program does not function as intended, because some of the x and y coordinates are 0 values.

#!/usr/bin/perl
use strict;

print "X,Y,Val\n";
foreach (@ARGV){
    open log,$_ or die $!;
    my ($x,$y,$val,$err);
    while(<log>){
        chomp;
        ($x,$y) = ($1,$2) if (/\((\d*|-\d*),(\d*|-\d*)\)/);
        ($val,$err) = ($1,$2) if (/^Results.*\((.*),(.*)\)$/);
        if (defined($x) && defined($y) && defined($val) && defined($err)){
            print "$x,$y,$val:$err\n";
            ($x,$y,$val,$err) = undef;
        }
    }
}

Thank you everyone for the answers, I’m learning a lot of new Perl syntax.
I’ve figured out how to get this script down to 10 lines. I was challenging myself on the number of lines in which I could write this.

#!/usr/bin/perl 
use strict;

print "X,Y,Val\n";
open LOG,"<@ARGV[0]" or die $!;
while(<LOG>){
    chomp;
    print "$1,$2," if (/\((\d*|-\d*),(\d*|-\d*)\)/);
    print "$1:$2\n" if (/^Results.*\((.*),(.*)\)$/);
}

Another Update. Using the information in the answers, I was able to get this down to 8 lines. I also improved the regex and made sure that the header would only be printed once if multiple files were provided.

#!/usr/bin/perl
use strict;

while(<>){
    print "X,Y,Val\n" if ($. == 1);
    print "$1,$2," if (/.*\((-?\d+),(-?\d+)\)/);
    print "$1:$2\n" if (/^Results.*\((.*)\).*\((.*)\)$/);
}
  • 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-23T21:34:31+00:00Added an answer on May 23, 2026 at 9:34 pm

    I would switch to reading two lines, rather than one:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use autodie;
    
    print "X,Y,Val\n";
    for my $filename (@ARGV) {
        open my $log, "<", $filename;
    
        while (my $coord_line = <$log>) {
            my ($x, $y) = $coord_line =~ /\((-?[0-9]+),(-?[0-9])\)/
                or die "bad coored line";
            my $results_line = <$log>;
            my ($val,$err) = $results_line =~ /^Results.*\((.*),(.*)\)$/
                or die "bad results line";
    
            print "$x,$y,$val:$err\n";
        }
    }
    

    One of the benefits of this approach is that your variables are now properly scoped. A simpler version of this program is:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use ARGV::readonly; #prevent files like "|ls" from breaking us
    
    print "X,Y,Val\n";
    while (<>) {
        my ($x, $y) = /\((-?[0-9]+),(-?[0-9]+)\)/
            or die "bad coored line";
        my ($val,$err) = <> =~ /^Results.*\((.*),(.*)\)$/
            or die "bad results line";
    
        print "$x,$y,$val:$err\n";
    }
    

    Another variant that takes into account the possibility of lines between the two lines we care about. It assumes the first coordinate pair is the right one:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use ARGV::readonly; #prevent files like "|ls" from breaking us
    
    print "X,Y,Val\n";
    while (<>) {
        next unless my ($x, $y) = /\((-?[0-9]+),(-?[0-9]+)\)/;
        my ($val, $err);
        while  (<>) {
            last if ($val, $err) = /^Results.*\((.*),(.*)\)$/;
        }
        die "bad format" unless defined $val;
        print "$x,$y,$val:$err\n";
    } 
    

    And this one handles the case where you want the last coordinate line:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use ARGV::readonly; #prevent files like "|ls" from breaking us
    
    print "X,Y,Val\n";
    my ($x, $y);
    while (<>) {
        ($x, $y) = ($1, $2) if /\((-?[0-9]+),(-?[0-9]+)\)/;
        next unless my ($val, $err) = /^Results.*\((.*),(.*)\)$/;
        print "$x,$y,$val:$err\n";
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a java program that needs a file open dialog. The file
I am writing an MPI program that needs to read a part of a
I am writing a program that needs to search a LARGE text document for
In a program that I'm writing, I wanted to make a ConfigParser that's read
I'm writing a program that contains a generational garbage collector. There are just two
I currently store a serialized XML file in the application directory that contains all
im writing an application that downloads and installs addons for programs which needs to
I'm writing a program that sends an email out at a client's specific local
I'm writing a program that uses SetWindowRgn to make transparent holes in a window
I am writing a program that will draw a solid along the curve of

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.