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

Discussion in 'Perl Misc' started by Alextophi, Sep 14, 2005.

  1. Alextophi

    Alextophi Guest

    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,
     
    Alextophi, Sep 14, 2005
    #1
    1. Advertising

  2. 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
     
    Mark Clements, Sep 14, 2005
    #2
    1. Advertising

  3. Alextophi

    Alextophi Guest

    Re: variable non initalisée / Use of uninitialized value in substr ...

    indeed it goes. Thank you
     
    Alextophi, Sep 14, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp Klaus Krause

    Value of uninitialized variable

    Philipp Klaus Krause, Jan 19, 2010, in forum: C Programming
    Replies:
    27
    Views:
    2,482
    Ben Bacarisse
    Jan 23, 2010
  2. Immortal Nephi

    Safe to use substr?

    Immortal Nephi, Jan 31, 2010, in forum: C++
    Replies:
    14
    Views:
    773
    James Kanze
    Feb 1, 2010
  3. Gavin Sinclair
    Replies:
    9
    Views:
    150
    Martin DeMello
    Dec 29, 2003
  4. Replies:
    4
    Views:
    167
    Tad McClellan
    Nov 2, 2006
  5. Daniel  Reardon
    Replies:
    1
    Views:
    110
    Richard Cornford
    Nov 20, 2006
Loading...

Share This Page