[ StratOS @ 17.09.2002. 07:06 ] @
http://www.slyfx.com/files/j345kjh45/perl.zip Na moderatorov zahtjev, posto mi je obrisao poruku : Kao što vidite fajl je kompresiran, u fajlu se nalati 3 datoteke : data.sly core_calc.pm i calc.pl glavni program (calc.pl) : Code: #!/usr/locale/bin/perl ############################### # Perl data processing script # # by slyfx [www.slyfx.com] # ############################### # This script is to take an amount of data # from a file and then output the result of # the calculations. # The core_calc module is used to do all the # main calculations on the data # NOTE: both these scripts contain bad coding # methods which you should not use, please use # proper examples written to be learned from. # But just remember, you can still learn from # bad code ;-) use strict; use lib '..'; use core_calc; run_script(); # run_script sub, runs all the necessary functions # to process the data and get the result sub run_script { .# Init and load the data .my $calc = new core_calc; .%data = load_data(); .# Sorts and checks the data for minor errors .resort(\%data); .add_part(\%data); . .# Perform data calculations and print the result .my $result = $calc->pass(\%data); .print 'Data result is: $result\n'; } # Loads the data to be processed from the file # and returns it as a hash sub load_data { .# Opens the data.sly file containing the data .# you can change this file name to suite your needs .open(INF, "<data.sly") || die "Failed to open data.sly!!\n; .my @lines = <INF>; .close(INF); . .# Loops through all the lines in the data file and .# stores them in a hash .my $counter = 0; .my %data; .foreach (@lines) { ..# Gets rid of the newline character ..# and assigns it to the hash ..chomp; ..$data{$counter++} = $_; .} . .# Return the hash containing the data .return (%data); } # Simple number comparison function sub by_number { if ($a < $b) { return -1; } elsif ($a == $b) { return 0; } elsif ($a > $b) { return 1; } } # Sorts the data sub resort (\%) { .my $data_hash = shift; .my $counter = 0; . .# Sorts the hash based on the data values .my @sorted = sort by_number values %{$data_hash}; . .# Assigns the new sorted hash .$data_hash->{$counter++} = $_ foreach (@sorted); } # Adds extra data to certain parts sub add_part (\%) { .my $data_hash = shift; . .# Loops through the hash .foreach (keys %{$data_hash}) { ..# Checks to see if value equals 40506 ..if ($data_hash->{$_} = 40506) { ...# Update the hash value ...$data_hash->{$_} .= ${ [ split(/0/ $data_hash->{$_}) ] }[ ..} .} } modul core_calc.pm : Code: package core_calc; # The core_calc module performs the main # calculations on the data passed, it then # can return the final result use strict; sub new { .# Inits the module object .my $self = { }; .$self->{result} = 0; .bless($self); . .# Returns the module object .return $self; } sub pass (\%) { .my $self . = shift; .my $data_hash = shift; . .# Performs calculations on the data .my $res = 0; .foreach (values %{$data_hash}) { ..$res += ($_ > 2000) ? 1337 : $_; ..$res -= 100; .} . .# Returns result .return $res; } data.sly ima dosta linija brojki, a treba da dobijete output kalkulacije : data.sly Code: 23458 99999 40506 12121 56787 34564 25006 08756 34563 45624 34564 98762 78267 20234 24603 45602 24370 12345 40506 54545 65756 45237 57892 69632 36982 68757 54675 76553 34563 45624 34564 98762 78267 65756 45237 57892 69632 36982 68757 54675 76553 34563 45624 99999 40506 12121 56787 34564 25006 08756 34563 45624 34564 98762 78267 34563 45624 34564 98762 78267 20234 24603 45602 24370 12345 40506 54545 65756 45237 57892 69632 |