User Control Panel
Advertisements

HELP US, HELP YOU!

Arrays

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Code Help
View unanswered posts
Author Message
JTW
God Like
God Like


Joined: 07 Mar 2004
Posts: 581
Location: Maidstone
Reputation: 73.2
votes: 4

PostPosted: Wed Feb 13, 2008 1:58 pm    Post subject: Arrays Reply with quote

Hi,
Im having trouble emptying an array. What I need to do is send message which are stored in the array.

Code:
   sub emptyMessageArray{
      for ($i=0;$i<50;$i++){
         my($to, $mirror,$message) = split(/ - /,  $messages[$i], 3);
         $bot->{msn}->{$mirror}->call($to,"$message");
         delete $messages[$i];
      }
   }

Basicly right now the whole sub is not working, it does not clear the array or send the messages. What am I doing wrong?

Thanks
JT

_________________
"Help us, Help you" - BotDepot
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Wed Feb 13, 2008 3:36 pm    Post subject: Reply with quote

You need to shift() or pop() the array; delete is for hashes.

push = add item to end of array
pop = remove item from end
unshift = add item to beginning
shift = remove item from beginning

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
JTW
God Like
God Like


Joined: 07 Mar 2004
Posts: 581
Location: Maidstone
Reputation: 73.2
votes: 4

PostPosted: Wed Feb 13, 2008 4:26 pm    Post subject: Reply with quote

Thanks,
I was also wondering if there was a way to find out what the highest 'number' of an array is, Not the highest content but how many individual bits of data are stored in it.
Beacuse what I am trying to do is get the attached sub to run through the array, send the message and then delete the data it has just sent. I am not sure if that make sense or not, but I am finding it kind of hard to explain.
Code:
   sub emptyMessageArray{
   my $i=@array;
      while ($i ne 0){
         my($to, $mirror,$message) = split(/-/,  $messages[$i], 3);
         print"To: $to\nMirror: $mirror\nMessage: $message\n\n\n";
         $bot->{msn}->{$mirror}->call($to,"$message");
         pop(@messages);
      }
   }

Since the code I posted in my original post I have changed the delete to pop, and modified the loop but I still cant get it to work.

Thanks

_________________
"Help us, Help you" - BotDepot
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Thu Feb 14, 2008 11:22 pm    Post subject: Reply with quote

Yeah,

scalar(@array) will return the number of elements in the array (starting at 1, so $array [ scalar(@array) - 1 ] is the last item in the array.

Why are you using a for $i loop anyway? Perl has a foreach loop (i.e. foreach my $item (@array) {}), but in this case you could just do a...

Code:
while (scalar(@array) > 0) {
   my $next = shift(@array);
   my ($to,$mirror,$msg) = split(/-/, $next, 3);
   ...
}

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
spx2
Newbie
Newbie


Joined: 07 Jan 2008
Posts: 2


PostPosted: Tue Jul 08, 2008 6:49 pm    Post subject: Re: Arrays Reply with quote

JTW wrote:

Code:
   sub emptyMessageArray{
      for ($i=0;$i<50;$i++){
         my($to, $mirror,$message) = split(/ - /,  $messages[$i], 3);
         $bot->{msn}->{$mirror}->call($to,"$message");
         delete $messages[$i];
      }
   }



you can modify htis source code to this

Code:
   sub emptyMessageArray{
      for ($i=0;$i<50;$i++){
         my($to, $mirror,$message) = split(/ - /,  $messages[$i], 3);
         $bot->{msn}->{$mirror}->call($to,"$message");
      }
              @messages=();
   }


but as Cer mentioned ,the approach with the shift operator is better and more natural when using Perl
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Code Help All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group