Schrift
[thread]11399[/thread]

if funktioniert nicht so wie ich mir vorstelle



<< >> 10 Einträge, 1 Seite
rom2mor
 2008-03-03 14:43
#106576 #106576
User since
2008-03-03
1 Artikel
BenutzerIn
[default_avatar]
Hallo zusammen,

ich bin ein totaler Anfänger in perl, jedoch habe ich mir gedacht, diese Zeile sollten problemlos funktionieren:
Code (perl): (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
my $currentperiod = 'P08';
print "\nCurrent Period is (LE period is current period incremented by one): ". $currentperiod;

# next twelve if are used to increment current period by one
if ($currentperiod eq 'P01') {
                my $LEperiod = 'P02';
}
if ($currentperiod eq 'P02') {
                my $LEperiod = 'P03';
}
if ($currentperiod eq 'P03') {
                my $LEperiod = 'P04';
}
if ($currentperiod eq 'P04') {
                my $LEperiod = 'P05';
}
if ($currentperiod eq 'P05') {
                my $LEperiod = 'P06';
}
if ($currentperiod eq 'P06') {
                my $LEperiod = 'P07';
}
if ($currentperiod eq 'P07') {
                $LEperiod = 'P08';
}
if ($currentperiod eq 'P08') {
                $LEperiod =~ 'P09';
}
if ($currentperiod eq 'P09') {
                my $LEperiod = 'P10';
}
if ($currentperiod eq 'P10') {
                my $LEperiod = 'P11';
}
if ($currentperiod eq 'P11') {
                my $LEperiod = 'P12';
}
if ($currentperiod eq 'P12') {
                my $LEperiod = 'P01';
}
print "\nCurrent Period after ifs is: ". $currentperiod;
$LEperiod ="xxxx";
print "\nDefault LE period is : " . $LEperiod . "\n\n";

==========

Ergebnise:

Current Period is (LE period is current period incremented by one): P08
Current Period after ifs is: P08
Default LE period is : xxxx



WAS läuft hier schief?

Vielen Dank, Alexandru
Taulmarill
 2008-03-03 14:48
#106577 #106577
User since
2004-02-19
1750 Artikel
BenutzerIn

user image
Evtl. möchtest du $LEperiod nicht mit "xxxx" überschreiben bevor du es ausgibst. Ansonsten währe es hilfreich zu wissen, was genau deiner Meinung nach schief läuft.
$_=unpack"B*",~pack"H*",$_ and y&1|0& |#&&print"$_\n"for@.=qw BFA2F7C39139F45F78
0A28104594444504400 0A2F107D54447DE7800 0A2110453444450500 73CF1045138445F4800 0
F3EF2044E3D17DE 8A08A0451412411 F3CF207DF41C79E 820A20451412414 83E93C4513D17D2B
Struppi
 2008-03-03 14:49
#106578 #106578
User since
2006-02-17
628 Artikel
BenutzerIn
[Homepage]
user image
Wieso was soll schief laufen?
renee
 2008-03-03 14:51
#106579 #106579
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Du verwendet lexikalische $LEperiods in den if-Blöcken und die sind nicht das gleiche wie das $LEperiod vor der Ausgabe...

Mach es mal so (du solltest außerdem entweder elsifs oder einen Hash verwenden):
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
my $currentperiod = 'P08';
print "\nCurrent Period is (LE period is current period incremented by one): ". $currentperiod;
my $LEperiod = 'xxxxx';

# next twelve if are used to increment current period by one
if ($currentperiod eq 'P01') {
                $LEperiod = 'P02';
}
elsif ($currentperiod eq 'P02') {
                $LEperiod = 'P03';
}
#... die anderen elsifs
elsif ($currentperiod eq 'P12') {
                $LEperiod = 'P01';
}
print "\nCurrent Period after ifs is: ". $currentperiod;
print "\nDefault LE period is : " . $LEperiod . "\n\n";


Edit: Fehler im Code beseitigt...
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/
Gast Gast
 2008-03-03 14:51
#106580 #106580
Fehler meinerseits:

die Anweisung "$LEperiod ="xxxx";" steht vor allen ifs

Ich wuerde erwarten, das LEperiod den Wert P08 einnimmt...
renee
 2008-03-03 14:54
#106581 #106581
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Lösung mit Hash:
Code (perl): (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
my $currentperiod = 'P08';
print "\nCurrent Period is (LE period is current period incremented by one): ". $currentperiod;
my $LEperiod = 'xxxxx';

my %hash = (
   P01 => 'P02',
   P02 => 'P03',
   P03 => 'P04',
   P04 => 'P05',
   P05 => 'P06',
   P06 => 'P07',
   P07 => 'P08',
   P08 => 'P09',
   P09 => 'P10',
   P10 => 'P11',
   P11 => 'P12',
   P12 => 'P01',
);

if( exists $hash{ $currentperiod } ){
   $LEperiod = $hash{$currentperiod};
}

print "\nCurrent Period after ifs is: ". $currentperiod;
print "\nDefault LE period is : " . $LEperiod . "\n\n";
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/
Struppi
 2008-03-03 14:57
#106582 #106582
User since
2006-02-17
628 Artikel
BenutzerIn
[Homepage]
user image
Du meinst wahrscheinlich P09, wie schon Renee sagte, das liegt daran, dass du $LEperiod mit my lokal machst und in Perl heißt das, im Block lokal also nur innerhalb der Klammern {....} ist die Änderung gültig.
renee
 2008-03-03 15:00
#106583 #106583
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Noch kürzer:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
my $currentperiod = 'P12';
print "\nCurrent Period is (LE period is current period incremented by one): ". $currentperiod;

my $LEperiod = $currentperiod;
++$LEperiod;
$LEperiod = 'P01' if $LEperiod eq 'P13';

print "\nCurrent Period after ifs is: ". $currentperiod;
print "\nDefault LE period is : " . $LEperiod . "\n\n";
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/
Gast Gast
 2008-03-03 15:58
#106589 #106589
Danke, es hat funktioniert!
KurtZ
 2008-03-03 21:18
#106600 #106600
User since
2007-12-13
411 Artikel
BenutzerIn
[default_avatar]
Noch kürzer:
Code (perl): (dl )
1
2
3
my $LEperiod = "P12";
$LEperiod = 'P01' if ++$LEperiod eq 'P13'; 
print $LEperiod'


eine der seltenen Fälle wo Magie lohnt ... allerdings IMHO dunkle Magie! :)

Auf P99 folgt Q00 und mit Decrement funzt es überhaupt nicht. Für kleine Beispiele lieber Hashes nehmen und bei größeren gut Dokumentieren, z.B. mit Verweis auf -> perlop#Auto_increment_and_Auto_decremen
TMTOWTDYOG (there's more than one way to dig your own grave)
<< >> 10 Einträge, 1 Seite



View all threads created 2008-03-03 14:43.