CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Start with empty array and add to it.

 



miniCruzer
Novice

Aug 8, 2010, 6:39 PM

Post #1 of 9 (1157 views)
Start with empty array and add to it. Can't Post

I'm trying to start with an empty array, called @names and add to it as input comes in. The environment is IRC. To start, I want the client to take /NAMES and store the names in an array called @names. Then, when a user attempts a command through the bot, it checks to see if user is opped. @names should read ('\@Bot', '\@User1', 'User2') where @ in front of a name is opped. When a user is opped, it should pop(@names, "\@User2"); and unshift(@names, "User2");. However, it's going to be reading from a line like:

Code
 if ($input =~ /:(.*)!(.*) MODE (.*) \+o (.*)/)

$4 will be the user.


shawnhcorey
Enthusiast


Aug 9, 2010, 6:03 AM

Post #2 of 9 (1142 views)
Re: [miniCruzer] Start with empty array and add to it. [In reply to] Can't Post

How do you guarantee that user will be the last name in @names? Perhaps you should be using a hash instead?

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


miniCruzer
Novice

Aug 9, 2010, 7:51 AM

Post #3 of 9 (1137 views)
Re: [shawnhcorey] Start with empty array and add to it. [In reply to] Can't Post

Is it not possible to read an array for any name (value)? A way to check if \@User1 is in @names?


shawnhcorey
Enthusiast


Aug 9, 2010, 8:22 AM

Post #4 of 9 (1135 views)
Re: [miniCruzer] Start with empty array and add to it. [In reply to] Can't Post

Yes but a hash would be faster.

Code
$names{$name} = 1;

Then later:

Code
if( $names{$user} ){ 
# user found
}else{
#user not found
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


miniCruzer
Novice

Aug 9, 2010, 9:06 AM

Post #5 of 9 (1133 views)
Re: [shawnhcorey] Start with empty array and add to it. [In reply to] Can't Post

Basically, its:
- Add user to hash when opped.
Then in a later if statement when a trigger for op-only comes
- Check to see if that user is opped.

I see how it would check to see if the user is in there, but not how it would add it.


Code
    if ($input =~ /:(.*)!(.*) MODE (.*) \+o (.*)/) 
{
# add $4 to hash
}


Then (this part is simplified, since I know what to do here)


Code
        if ($input =~ /:(.*)!(.*) PRIVMSG (.*) :!restart/) 
{
if ($names{$user}) {
# restart
} else {
# deny
}

}



(This post was edited by miniCruzer on Aug 9, 2010, 9:26 AM)


shawnhcorey
Enthusiast


Aug 9, 2010, 9:44 AM

Post #6 of 9 (1125 views)
Re: [miniCruzer] Start with empty array and add to it. [In reply to] Can't Post


Code
   if ($input =~ /:(.*)!(.*) MODE (.*) \+o (.*)/)  
{
$names{$4} = 1;
}



Code
        if ($input =~ /:(.*)!(.*) PRIVMSG (.*) :!restart/)  
{
if ($names{$user}) {
# restart
} else {
# deny
}

}


Replace $user with the correct transient variable: $1 or $2 or $3

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


miniCruzer
Novice

Aug 9, 2010, 10:15 AM

Post #7 of 9 (1124 views)
Re: [shawnhcorey] Start with empty array and add to it. [In reply to] Can't Post

Thanks so much. So far there is 1 stipulation.

The way I have this set up is using strict and warnings. So in a separate file called Variables is:

Code
our $names{$name} = 1;

It doesn't like $name, and won't accept:

Code
our $names{our $name} = 1;

and returns: syntax error at src/Variables.pm line 31, near "$names{".

Without our in front of $name, I get: Global symbol "$name" requires explicit package name at src/Variables.pm line 29.


shawnhcorey
Enthusiast


Aug 9, 2010, 10:18 AM

Post #8 of 9 (1121 views)
Re: [miniCruzer] Start with empty array and add to it. [In reply to] Can't Post

You should declare your variables at the start of the program.

Code
our $name = ''; 
our $user = '';
our %names = ();


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


miniCruzer
Novice

Aug 9, 2010, 10:51 AM

Post #9 of 9 (1115 views)
Re: [shawnhcorey] Start with empty array and add to it. [In reply to] Can't Post

Are you sure that our $names{$4} = 1; is the proper syntax? It gives me:

Code
syntax error at src/Main.pm line 61, near "$names{"


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives