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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Help

 



yaroba
Novice

May 27, 2013, 12:43 AM

Post #1 of 2 (595 views)
Help Can't Post

Hello, could anyone take a look at my code ? I am having problems while using List::Util function.

I am writing a program, that: puts integer between brackets, shows count of integers, counts their sum also finds biggest/smallest integer.

use warnings;
use List::Util qw(min max sum);

my $string = 0;
my $count = 0;
my $suma = 0;

open my $FILE, '<', 'skaiciai.txt' or die "Negalu atidaryti: $!";

while (my $string = <$FILE>) {
$string =~ s/([-+.\d]+)/<$1>/g;
print "$string\n";
}

open my $fh, '<', 'skaiciai.txt' or die "Negalu atidaryti: $!";

while (<$fh>) {
$count++ for split /[ ,.]+/;

}

use List::Util qw(min max sum);

my @skaiciai = <skaiciai.txt>;
$suma = sum @skaiciai;
$max = max @skaiciai;
$min = min @skaiciai;


print "Suma $suma\nFaile yra $count skaiciai\n";


BillKSmith
Veteran

May 27, 2013, 6:27 AM

Post #2 of 2 (579 views)
Re: [yaroba] Help [In reply to] Can't Post

There are two very different ways to solve this problem. Your code mixes in pieces of each.

The first method is usually easier for small data files. It 'slurps' the entire file into an array. Closes the file. Processes the array.

The other method uses a loop to read one line from the file into a scalar. That scalar is used to update results. The loop continues until the entire file has been read and processed. The file can then be closed. This method usually requires much less memory becuse it does not store the file. It easily scales up to files of any size.

The following examples are designed to show the similarities and differences.

CASE 1 (Slurp)

Code
use strict; 
use warnings;
use List::Util qw(min max sum);

open my $FILE, '<', 'skaiciai.txt' or die "Negalu atidaryti: $!";
chomp( my @integers = <$FILE> ); # Slurp entire file
close $FILE;
my @bracketed = map {"<$_>"} @integers;
my $max = max(@integers);
my $min = min(@integers);
my $suma = sum(@integers);
print "@bracketed\n";
print "max = $max\n",
"min = $min\n",
"sum = $suma\n",
"mean = ", $suma/@integers,
;


CASE 2 (Line-by-line processing)

Code
use strict; 
use warnings;
use List::Util qw(min max sum);

my $min = 2**31 - 1;
my $max = -$min;
my $suma = 0;
my $count = 0;
my @bracketed;
open my $FILE, '<', 'skaiciai.txt' or die "Negalu atidaryti: $!";
while ( my $integer = <$FILE> ) {
chomp $integer;
push @bracketed, "<$integer>";
$max = max( $max, $integer );
$min = min( $min, $integer );
$suma = sum( $suma, $integer );
$count++;
}
close $FILE;
print "@bracketed\n";
print "max = $max\n",
"min = $min\n",
"sum = $suma\n",
"mean = ", $suma/$count,
;

Good Luck,
Bill

 
 


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

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