Schrift
[thread]555[/thread]

CGI: tailor an existing programme



<< >> 3 Einträge, 1 Seite
Crian
 2005-04-04 18:41
#5468 #5468
User since
2003-08-04
5866 Artikel
ModeratorIn
[Homepage]
user image
Hi,

this forum is a german forum. And we have a special CGI sub forum. And please add code tags to your post.
s--Pevna-;s.([a-z]).chr((ord($1)-84)%26+97).gee; s^([A-Z])^chr((ord($1)-52)%26+65)^gee;print;

use strict; use warnings; Link zu meiner Perlseite
Gast Gast
 2005-04-04 18:35
#5469 #5469
Dear

This program created a txt file from tape info in the database longlife. It creates txt files when i give a range numbers. For example when i give M3225 to M3227: it creates 3 differentes txt files (M3225.txt, M3226.txt and M3227.txt). My problem now is to create just one txt file with all these infos. Example when i give M3225 to M3227, it will create just M3225 and have M3226 and M3227 inside.

Please help me somebody.

Thank you.

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Includes
use DBI;
use CGI qw(:standard);
use strict "vars";

# Init values
my $dsn = "DBI:mysql:longlife:localhost"; # data source name
my $user_name = "longlife"; # user name
my $password = "choices"; # password
my $out = "/home/intel/loglists/TXT/"; # path to the output directory
my $template = "/var/www/html/tech/test_loglistext.html"; # path to output template (design only)
my($sth,$dbh,@work_done); # declare global vars
system("unset LANG"); # remove any specific language settings of the system
print "Content-type: text/html;\n\n";

##### Start main

# get tape numbers from user
my $tape = param("tape");
my $tape1 = param("tape1");

chomp($tape);
chomp($tape1);

# connect to database
$dbh = DBI->connect ($dsn, $user_name, $password, { RaiseError => 1 });
# check if tape numbers start with a letter
# if yes, it is assumed that both numbers start with the same letter
my $pref;
if($tape =~ /^\D/){
# store tape nuber and prefix letter separately
$pref = substr($tape,0,1);
$tape =~ s/^\D//;

}
if($tape1 =~ /^\D/){$tape1 =~ s/^\D//}

##@@@ Main Loop
# iterate trough the whole range and create txt files where possible

for(my $i=$tape;$i<=$tape1;$i++){

# issue query
$sth = $dbh->prepare("SELECT the_number,',',client_company_name,',',name,',',_Title,',',_Version,',',_PGM_Duration,',',_Tape_Format FROM tapes_2 WHERE the_number='$pref$i'");

$sth->execute ();

my $output = $sth->fetchall_arrayref;
my @output = @$output;

if($output[0]){
open(OUT,">$out$pref$i.txt") or die "could not open output file";
print OUT <<"end_txt";
end_txt

foreach my $row (@output){
print OUT $row->[0];
print OUT $row->[1];
print OUT $row->[2];
print OUT $row->[3];
print OUT $row->[4];
print OUT $row->[5];
print OUT $row->[6];
print OUT $row->[7];
print OUT $row->[8];
print OUT $row->[9];
print OUT $row->[10];
print OUT $row->[11];
print OUT $row->[12];
}

close(OUT);
chmod(0777,$file);
# save tape number for later displaying
push(@work_done, "$pref$i");

}

}


# show process information to user
open(TEMPL,"$template");
my @template = <TEMPL>;
close(TEMPL);
foreach my $line (@template){print $line}

print "<br>&nbsp;&nbsp;&nbsp;<b>Tapes processed:</b><br>";
foreach my $tape (@work_done){
print "<br>&nbsp;&nbsp;&nbsp;$tape";
}
print "</body></html>";

## finish up
$sth->finish ();
$dbh->disconnect ();
exit;


edit @Relais: Code-Tags\n\n

<!--EDIT|Relais|1112628296-->
renee
 2005-04-04 20:15
#5470 #5470
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
You can open a Filehandle before you do your database queries. So you have only one textfile for all three tapes.

Within your loop, you have to print your results to the filehandle without opening a new filehandle!

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
open(OUT,">/path/to/file.txt") or die $!;
for(my $i=$tape;$i<=$tape1;$i++){

# issue query
$sth = $dbh->prepare("SELECT the_number,',',client_company_name,',',name,',',_Title,',',_Version,',',_PGM_Duration,',',_Tape_Format FROM tapes_2 WHERE the_number='$pref$i'");

$sth->execute ();

my $output = $sth->fetchall_arrayref;
my @output = @$output;

if($output[0]){
print OUT <<"end_txt";
end_txt

foreach my $row (@output){
print OUT $row->[0];
print OUT $row->[1];
print OUT $row->[2];
print OUT $row->[3];
print OUT $row->[4];
print OUT $row->[5];
print OUT $row->[6];
print OUT $row->[7];
print OUT $row->[8];
print OUT $row->[9];
print OUT $row->[10];
print OUT $row->[11];
print OUT $row->[12];
}

chmod(0777,$file);
# save tape number for later displaying
push(@work_done, "$pref$i");

}

}

close(OUT);


You should also read the documentation of CPAN:DBI... Use the ?-notation for your queries. This will avoid a lot of errors...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
<< >> 3 Einträge, 1 Seite



View all threads created 2005-04-04 18:41.