http://www.perlmonks.org?node_id=840900

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

please help me to find the solution of this error

1- Modification of non-creatable array value attempted, subscript -1 at research.pl line 94.

############################################ # VARIABLES ############################################ .... # array scotching sms which was readed my @msg2 = (); .......... ############################################ # START EXECUTION ############################################ answer_sms(); ########################################### # answer_sms() ########################################### sub answer_sms { #Build gsm object $gsm = new Device::Gsm(port=>'/dev/cuaU0'); # read SMS of Sim card @msg = $gsm->messages('SM'); # get the number of sms in the card $taille = @msg; # read the old sms in my sim card proc_read_card(); # creat array for stoching nmbr of sms in the card @tab=($taille); print "\nWAITING FOR NEW SMS.......\n"; while(1) { my $newtime = (time+50); @msg2 =$gsm->messages('SM'); $taille2 = @msg2; push(@tab,$taille2); if($tab[-1] != $tab[-2]) { # get the number of the last sender # following the LINE 94# my $dernier_num = $msg2[$#msg2]->sender(); ................ $gsm->send_sms(recipient=>$dernier_num,content=>"Error" +); ............. } }
when the last line is executed i get the error. Thank you in advance

Replies are listed 'Best First'.
Re: Modification of non-creatable array
by Krambambuli (Curate) on May 20, 2010 at 11:57 UTC
    Maybe you just need to exit the endless loop somehow ?

    [...] @msg2 =$gsm->messages('SM'); last if @msg2 == 0; [...]
    Otherwise, lifting the secret about which line exactly is line #94 would probably do no harm to the code's privacy ;)


    Krambambuli
    ---

      Thank's Krambambuli it's work.

      line #94 : # get the number of the last sender my $dernier_num = $msg2[$#msg2]->sender();

      it was just just for not being long :) !

      ps: i have another question about the same code should i creat new thread or continue in the same ?
          ps: i have another question about the same code should i creat new thread or continue in the same ?

        Well, hold on for a minute; think about what a good title for the problem would be and then decide accordingly. If the current title fits the problem, stay in the thread; if not, not.


        Krambambuli
        ---
Re: Modification of non-creatable array
by FunkyMonk (Chancellor) on May 20, 2010 at 11:59 UTC
    My guess is that
    @msg2 =$gsm->messages('SM');

    didn't return any messages, which means that $#msg2 is -1.

    Update:

    And at line 94...

    my $dernier_num = $msg2[$#msg2]->sender(); # ^^^^^^

    You're trying to access the last element of an empty array!

      the @msg2 is's not empty, the probleme was to get out of the loop.thank you funkymonk