Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > [FR/EN] variable non initalisée / Use of uninitialized value in substr ...

Reply
Thread Tools

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

 
 
Alextophi
Guest
Posts: n/a
 
      09-14-2005
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,

 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      09-14-2005
Alextophi wrote:
> 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
 
Reply With Quote
 
 
 
 
Alextophi
Guest
Posts: n/a
 
      09-14-2005
indeed it goes. Thank you

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Safe to use substr? Immortal Nephi C++ 14 02-01-2010 08:42 PM
Value of uninitialized variable Philipp Klaus Krause C Programming 27 01-23-2010 03:07 AM
Can't get a substr() or substring() of a passed in variable Daniel Reardon Javascript 1 11-20-2006 09:39 AM
Use of uninitialized variable with print << HTMLEND (and $ENV) kevinwhite@bellsouth.net Perl Misc 4 11-02-2006 03:18 PM
Inconsistent value of uninitialized variable Gavin Sinclair Ruby 9 12-29-2003 08:55 AM



Advertisments