Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Modification of non-creatable array

by Anonymous Monk
on May 20, 2010 at 11:16 UTC ( #840900=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Modification of non-creatable array
Download Code
Re: Modification of non-creatable array
by Krambambuli (Deacon) 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 (Canon) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://840900]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2015-07-02 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls