[FR/EN] variable non initalisée / Use of uninitialized value in substr ...

A

Alextophi

FR ---------------------------------------------------------
bonjour

j'ai un problème d'initialisation de variable $SCHED[$v] ($v=0 à 7),
mais je ne sais pas comment faire pour y remedier.

La variable $SCHED[$v] est parfois vide, et donc j'ai un message
d'erreur 'Use of uninitialized value in ...'


d'avance merci

christophe



EN ---------------------------------------------------------

hello

I have a problem of initialization of variable $SCHED[$v] ($v=0 to 7),
but I do not know how to make to solve.
The variable is sometimes empty, and thus I have an error message

in advance thank you

christophe

-CODE--------------------------------------------------------------------
471
472
473
474 my @SCHED = ();
475 @SCHED = split(" ", $SCHED_A);
476
477 for ($v=0; $v<7; $v++) {
478
479 if (substr($SCHED[$v],0,2) eq "Lu") { $SCHED[$v] = "Lundi" };
480 if (substr($SCHED[$v],0,2) eq "Ma") { $SCHED[$v] = "Mardi" };
481 if (substr($SCHED[$v],0,2) eq "Me") { $SCHED[$v] = "Mercredi"};
482 if (substr($SCHED[$v],0,2) eq "Je") { $SCHED[$v] = "Jeudi" };
483 if (substr($SCHED[$v],0,2) eq "Ve") { $SCHED[$v] = "Vendredi"};
484 if (substr($SCHED[$v],0,2) eq "Sa") { $SCHED[$v] = "Samedi" };
485 if (substr($SCHED[$v],0,2) eq "Di") { $SCHED[$v] = "Dimanche"};
486 if (substr($SCHED[$v],0,3) eq "Tlj") {$SCHED[$v] =
"Tous_les_jours"};
487
488 }

-RESULT-------------------------------------------------------------------

Use of uninitialized value in substr at Collecte_Networker.pl line 479,

Use of uninitialized value in substr at Collecte_Networker.pl line 480,

Use of uninitialized value in substr at Collecte_Networker.pl line 481,

Use of uninitialized value in substr at Collecte_Networker.pl line 482,

Use of uninitialized value in substr at Collecte_Networker.pl line 483,

Use of uninitialized value in substr at Collecte_Networker.pl line 484,

Use of uninitialized value in substr at Collecte_Networker.pl line 485,

Use of uninitialized value in substr at Collecte_Networker.pl line 486,
 
M

Mark Clements

Alextophi said:
FR ---------------------------------------------------------
bonjour

j'ai un problème d'initialisation de variable $SCHED[$v] ($v=0 à 7),
mais je ne sais pas comment faire pour y remedier.

La variable $SCHED[$v] est parfois vide, et donc j'ai un message
d'erreur 'Use of uninitialized value in ...'


d'avance merci

christophe



EN ---------------------------------------------------------

hello

I have a problem of initialization of variable $SCHED[$v] ($v=0 to 7),
but I do not know how to make to solve.
The variable is sometimes empty, and thus I have an error message

in advance thank you

christophe

-CODE--------------------------------------------------------------------
471
472
473
474 my @SCHED = ();
475 @SCHED = split(" ", $SCHED_A);
476
477 for ($v=0; $v<7; $v++) {
478
479 if (substr($SCHED[$v],0,2) eq "Lu") { $SCHED[$v] = "Lundi" };
480 if (substr($SCHED[$v],0,2) eq "Ma") { $SCHED[$v] = "Mardi" };
481 if (substr($SCHED[$v],0,2) eq "Me") { $SCHED[$v] = "Mercredi"};
482 if (substr($SCHED[$v],0,2) eq "Je") { $SCHED[$v] = "Jeudi" };
483 if (substr($SCHED[$v],0,2) eq "Ve") { $SCHED[$v] = "Vendredi"};
484 if (substr($SCHED[$v],0,2) eq "Sa") { $SCHED[$v] = "Samedi" };
485 if (substr($SCHED[$v],0,2) eq "Di") { $SCHED[$v] = "Dimanche"};
486 if (substr($SCHED[$v],0,3) eq "Tlj") {$SCHED[$v] =
"Tous_les_jours"};
487
488 }

-RESULT-------------------------------------------------------------------

Use of uninitialized value in substr at Collecte_Networker.pl line 479,

<snip>
Don't go into the for loop if @SCHED isn't set.

if(@SCHED){
# for loop goes here
}

Also, you aren't doing your ifs cleanly: eg if the substring is equal to
"Lu" then it can't also be equal to "Ma".

*but*

this should be some form of lookup anyway.

eg

my %dayAbbreviations = (
Lu => "Lundi",
Ma => "Mardi",
# etc

);

You should probably be using a more perlish construct like:

foreach my $schedule(@SCHED){
my $fullDayName = $dayAbbreviations{substr($schedule,0,2)};
if($fullDayName){
$schedule = $fullDayName;
}else{
# handle unexpected data
}

}

though I'd probably load the days and their abbreviations from an
external source.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top