M
Marek Stepanek
Hello happy Perlers,
a beginner has to struggle with many mysterious problems. One of these
problems is, to ask a question shortly, so excuse me for this long posting.
I have tried to put in a script a random number. I read the perlfaq4 ("How
do I get a random number between X and Y?") and made my first "random steps"
with this script, which is working fine:
#!/usr/bin/perl
# here a subroutine found in: % perldoc perlfaq4
my $random_number = random_int_in(50,120);
sub random_int_in ($$)
{
my($min, $max) = @_;
# Assumes that the two arguments are integers themselves!
return $min if $min == $max;
($min, $max) = ($max, $min) if $min > $max;
return $min + int rand(1 + $max - $min);
}
print "here it comes: my first random number, I hope: \n\t$random_number
!!\n\nWOW! It's workin \n\n";
my $random_number2 = random_int_in(1,7);
my $random_number3 = "1.$random_number2";
print "there is coming a second random Number! \n\t$random_number2\n\nWOW!
It's still workin \n\n";
print "there is coming a third random Number! \n\t$random_number3\n\nWOW!
It's still workin \n\n";
I am integrating it into my script, producing a LaTeX - file, but I get
mysterious error messages:
main::random_int_in() called too early to check prototype at ./calc_hours.pl
line 54.
Argument "185.88 / 1.8" isn't numeric in sprintf at ./calc_hours.pl line 63,
<IN> line 61 ...
This script is long, sorry:
#!/usr/bin/perl
use strict;
use warnings;
#### Files #################
my $in_file = 'calc_hours_in.tex';
open IN, "< $in_file" or die "Could not open your in_file: $!";
my $out_file = 'calc_hours.tex';
open OUT, "> $out_file" or die "Connot create your out_file: $!";
####END Files ##############
my $latex_head = <<"EOF";
\\documentclass[a4]{scrartcl}
\\usepackage[utf8]{inputenc}
\\usepackage{ltxtable}
\\clubpenalty = 10000
\\widowpenalty = 10000
\\begin{document}
Fahrer\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\hfill
{Monat}\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_
\\begin{longtable}[c]{| c | c | c | c | c | c | c | c |}
\\hline
\\multicolumn{8}{| c |}{\\textbf{Tageseinnahmen/Arbeitsnachweis}} \\\\
\\hline
& & & \\multicolumn{5}{ c |}{\\textbf{Arbeitszeit}} \\\\
\\hline
\\textbf{Datum} & \\textbf{Umsatz 7\\%} & \\textbf{Umsatz 16\\%} &
\\textbf{Beginn} & \\textbf{Pause} & \\textbf{Ende} & \\textbf{Stunden} &
\\textbf{Km-Gesamt} \\\\
\\hline
EOF
my $latex_foot = <<EOF;
\\end{longtable}
\\end{document}
EOF
print OUT "$latex_head";
####Variables###############
my $date1 = '';
my $date2 = '';
my @lines_in = '';
my @lines_out = '';
my $sum = 0;
my $sum_total = 0;
my $sum_total_sb = 0;
####End Variables###########
####Read in and work########
while ( <IN> ) {
/^(\w+, +[\d.]+)/ and $date1 = $1;
if (/^TOTAL/) { push @lines_in, "$date1\t$_"};
}
my $random_number = random_int_in(5,8);
foreach my $line (@lines_in) {
($sum) = $line =~ /([\d.]+)\s+$/;
($date2) = $line =~ /^(\w+, +[\d.]+)/;
$sum_total += $sum if $sum;
$sum *= 0.40 if $sum;
$sum_total_sb += $sum if $sum;
my $random_km = "1.$random_number";
my $km_tag = "$sum / $random_km" if $sum;
$km_tag = sprintf("%.2f", $km_tag) if $sum;
my $begin = 6;
my $end = $begin + 8;
my $hours = $end - $begin;
push (@lines_out,
"$date2\t&\t$sum\t&\tUmsatz16\t&\t$begin\t&\t$random_km\t&\t$end\t&\t$hours\
t&\t$km_tag\\\\\n" ) if $sum;
}
####END Read in and work####
####Output##################
print OUT "@lines_out";
print OUT "Gesamt:\t&\t$sum_total_sb\t&\t&\t&\t&\t&\t&\\\\\n";
print OUT $latex_foot;
####END Output##############
sub random_int_in ($$)
{
my($min, $max) = @_;
# Assumes that the two arguments are integers themselves!
return $min if $min == $max;
($min, $max) = ($max, $min) if $min > $max;
return $min + int rand(1 + $max - $min);
}
close OUT;
close IN;
The IN-File ( calc_hours_in.tex ) contains many lines, from which I read in
only the TOTALs and work on it ...
Son, 16.07.2006 37086.40 15445.00 808 19.50 3156.30
Mon, 17.07.2006 37667.00 15769.20 817 19.50 3621.00
TOTAL 580.60 324.20 9 0.00 464.70
Mon, 17.07.2006 37667.00 15769.20 817 19.50 3621.00
Die, 18.07.2006 37936.50 15929.60 823 19.50 3857.80
TOTAL 269.50 160.40 6 0.00 236.80
a beginner has to struggle with many mysterious problems. One of these
problems is, to ask a question shortly, so excuse me for this long posting.
I have tried to put in a script a random number. I read the perlfaq4 ("How
do I get a random number between X and Y?") and made my first "random steps"
with this script, which is working fine:
#!/usr/bin/perl
# here a subroutine found in: % perldoc perlfaq4
my $random_number = random_int_in(50,120);
sub random_int_in ($$)
{
my($min, $max) = @_;
# Assumes that the two arguments are integers themselves!
return $min if $min == $max;
($min, $max) = ($max, $min) if $min > $max;
return $min + int rand(1 + $max - $min);
}
print "here it comes: my first random number, I hope: \n\t$random_number
!!\n\nWOW! It's workin \n\n";
my $random_number2 = random_int_in(1,7);
my $random_number3 = "1.$random_number2";
print "there is coming a second random Number! \n\t$random_number2\n\nWOW!
It's still workin \n\n";
print "there is coming a third random Number! \n\t$random_number3\n\nWOW!
It's still workin \n\n";
I am integrating it into my script, producing a LaTeX - file, but I get
mysterious error messages:
main::random_int_in() called too early to check prototype at ./calc_hours.pl
line 54.
Argument "185.88 / 1.8" isn't numeric in sprintf at ./calc_hours.pl line 63,
<IN> line 61 ...
This script is long, sorry:
#!/usr/bin/perl
use strict;
use warnings;
#### Files #################
my $in_file = 'calc_hours_in.tex';
open IN, "< $in_file" or die "Could not open your in_file: $!";
my $out_file = 'calc_hours.tex';
open OUT, "> $out_file" or die "Connot create your out_file: $!";
####END Files ##############
my $latex_head = <<"EOF";
\\documentclass[a4]{scrartcl}
\\usepackage[utf8]{inputenc}
\\usepackage{ltxtable}
\\clubpenalty = 10000
\\widowpenalty = 10000
\\begin{document}
Fahrer\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\hfill
{Monat}\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_
\\begin{longtable}[c]{| c | c | c | c | c | c | c | c |}
\\hline
\\multicolumn{8}{| c |}{\\textbf{Tageseinnahmen/Arbeitsnachweis}} \\\\
\\hline
& & & \\multicolumn{5}{ c |}{\\textbf{Arbeitszeit}} \\\\
\\hline
\\textbf{Datum} & \\textbf{Umsatz 7\\%} & \\textbf{Umsatz 16\\%} &
\\textbf{Beginn} & \\textbf{Pause} & \\textbf{Ende} & \\textbf{Stunden} &
\\textbf{Km-Gesamt} \\\\
\\hline
EOF
my $latex_foot = <<EOF;
\\end{longtable}
\\end{document}
EOF
print OUT "$latex_head";
####Variables###############
my $date1 = '';
my $date2 = '';
my @lines_in = '';
my @lines_out = '';
my $sum = 0;
my $sum_total = 0;
my $sum_total_sb = 0;
####End Variables###########
####Read in and work########
while ( <IN> ) {
/^(\w+, +[\d.]+)/ and $date1 = $1;
if (/^TOTAL/) { push @lines_in, "$date1\t$_"};
}
my $random_number = random_int_in(5,8);
foreach my $line (@lines_in) {
($sum) = $line =~ /([\d.]+)\s+$/;
($date2) = $line =~ /^(\w+, +[\d.]+)/;
$sum_total += $sum if $sum;
$sum *= 0.40 if $sum;
$sum_total_sb += $sum if $sum;
my $random_km = "1.$random_number";
my $km_tag = "$sum / $random_km" if $sum;
$km_tag = sprintf("%.2f", $km_tag) if $sum;
my $begin = 6;
my $end = $begin + 8;
my $hours = $end - $begin;
push (@lines_out,
"$date2\t&\t$sum\t&\tUmsatz16\t&\t$begin\t&\t$random_km\t&\t$end\t&\t$hours\
t&\t$km_tag\\\\\n" ) if $sum;
}
####END Read in and work####
####Output##################
print OUT "@lines_out";
print OUT "Gesamt:\t&\t$sum_total_sb\t&\t&\t&\t&\t&\t&\\\\\n";
print OUT $latex_foot;
####END Output##############
sub random_int_in ($$)
{
my($min, $max) = @_;
# Assumes that the two arguments are integers themselves!
return $min if $min == $max;
($min, $max) = ($max, $min) if $min > $max;
return $min + int rand(1 + $max - $min);
}
close OUT;
close IN;
The IN-File ( calc_hours_in.tex ) contains many lines, from which I read in
only the TOTALs and work on it ...
Son, 16.07.2006 37086.40 15445.00 808 19.50 3156.30
Mon, 17.07.2006 37667.00 15769.20 817 19.50 3621.00
TOTAL 580.60 324.20 9 0.00 464.70
Mon, 17.07.2006 37667.00 15769.20 817 19.50 3621.00
Die, 18.07.2006 37936.50 15929.60 823 19.50 3857.80
TOTAL 269.50 160.40 6 0.00 236.80