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 578321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:14:51+00:00 2026-05-13T14:14:51+00:00

I have a datas et and like to do a simple while operation with

  • 0

I have a datas et and like to do a simple while operation with a Perl script.
Here is a small extraction from the dataset:

“number”,”code”,”country”,”gamma”,”X1″,”X2″,”X3″,”X4″,”X5″,”X6″
1,”DZA”,”Algeria”,”0.01″,7.44,47.3,0.46,0,0,0.13
2,”AGO”,”Angola”,”0.00″,6.79,”NULL”,0.21,1,0,0.28
3,”BEN”,”Benin”,”-0.01″,7.02,38.9,0.27,1,0,0.05
4,”BWA”,”Botswana”,”0.06″,6.28,45.7,0.42,1,0,0.07
5,”HVO”,”Burkina Faso”,”0.00″,6.15,36.3,0.08,1,0,0.05
6,”BDI”,”Burundi”,”0.00″,6.38,41.8,0.18,1,0,0

The script should count the length of every , separated field and store the highest values
into an array.

However, the saving doesn’t work properly. Here is a part of the code:

@maxl = map length, @terms;

while(`<INFILE>`) {
$_ =~ s/[\"\n]//g ;
@terms = split/$sep/, $_;
@lengths = map length, @terms;
for($k = 0, $k <= $#terms, $k++) { 
    if($lengths[$k] > $maxl[$k]) {
    $maxl[$k] = $lenghts[$k];
    }
}
print "@lengths\n";
}

Now the @maxl uses an earlier part from the code where it uses the second line of the dataset.
When I use a print command just to see the values of the @maxl operation i get:

1 3 7 4 4 4 4 1 1 5

In the while loop I used another print statement just to see the other values, I get:

1 3 6 4 4 4 4 1 1 4
1 3 5 5 4 4 4 1 1 4
1 3 8 4 4 4 4 1 1 4
1 3 12 4 4 4 4 1 1 4
1 3 7 4 4 4 4 1 1 1
1 3 8 4 4 4 4 1 1 4
1 3 10 4 4 4 4 1 1 4
1 3 16 5 4 4 4 1 1 4
2 3 4 5 3 4 4 1 1 4
2 3 7 4 4 4 4 1 1 4
2 3 5 4 4 4 4 1 1 4
2 3 5 4 4 4 4 1 1 4
2 3 8 4 4 4 4 1 1 4
2 3 5 4 4 4 1 1 1 4

The fourth column eg has obviously values which are greater than 3. The while loop was supposed to save the greatest values and substitute those values into @maxl.

What went wrong?


…in the for loop the comma are wrong

for($k = 0, $k <= $#terms, $k++)

however, after cleaning that up there still seems to be a problem…

  • 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-13T14:14:52+00:00Added an answer on May 13, 2026 at 2:14 pm

    there’s a typo here
    $maxl[$k] = $lenghts[$k];
    for starters (which ‘use strict’ would have caught)

    consider using Text::CSV for more reliable parsing of comma-separated data (it can also handle other separators):

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Text::CSV;
    
    my $csv = Text::CSV->new();
    my @max_lengths;
    
    while ( my $line = <INFILE> ) {
    
        die "Unable to parse '$line'" unless $csv->parse($line);
    
        my @column_lengths = map { length } $csv->fields();
    
        for my $i ( 0 .. $#column_lengths ) {
            if ( $column_lengths[$i] > ($max_lengths[$i] || 0) ) {
                $max_lengths[$i] = $column_lengths[$i];
            }
        }
    }
    
    print "MAX LENGTHS OF EACH FIELD: @max_lengths\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Click on the "show details" link under Date (dd/mm/YYY), then… May 14, 2026 at 9:11 pm
  • Editorial Team
    Editorial Team added an answer Note that you are echoing and in false condition it… May 14, 2026 at 9:11 pm
  • Editorial Team
    Editorial Team added an answer SELECT f1,f2, xxx FROM (SELECT *, 'tbl1' as xxx FROM… May 14, 2026 at 9:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.