4 Replies - 6596 Views - Last Post: 03 July 2012 - 06:01 PM

#1 shealyw2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-June 12

Hash and List return from a subroutine of a Library.

Posted 30 June 2012 - 01:07 PM

Pretty new to perl:

I am using the POE:COMPONENT:IRC:STATE library for a bot i am writing. I am trying to use the channel_list function:

sub channel_list {
    my ($self, $chan) = @_;

    if (!defined $chan) {
        warn 'Channel is undefined';
        return;
    }

    my $map   = $self->isupport('CASEMAPPING');
    my $uchan = uc_irc($chan, $map);

    return if !$self->_channel_exists($chan);
    return map { $self->{STATE}{Nicks}{$_}{Nick} } keys %{ $self->{STATE}{Chans}{ $uchan }{Nicks} };
}


I call it
$irc_Channel_list(channel);


My question is how do i get the return, i don't know the size of the list. It seems to be handeling that somehow, but i am so new i am not 100% sure on what the return is doing.

Is This A Good Question/Topic? 0
  • +

Replies To: Hash and List return from a subroutine of a Library.

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2086
  • View blog
  • Posts: 3,173
  • Joined: 21-June 11

Re: Hash and List return from a subroutine of a Library.

Posted 30 June 2012 - 01:36 PM

Quote

My question is how do i get the return


You can use the return value of a subroutine by either using it right away (like passing it to another function or using it inside an arithmetic expression or the like) or storing it in a variable. In this case you probably want to do the latter. For an array this will look like this:

my @nicks = $irc->channel_list(channel);



You can then work with @nicks like you would with any other array variable.

Quote

i don't know the size of the list


You can find out the length of @nicks, by using it in a scalar context. For example you can store the length in a variable like this:

my $length_of_nicks = @nicks;


This post has been edited by sepp2k: 30 June 2012 - 01:38 PM

Was This Post Helpful? 3
  • +
  • -

#3 shealyw2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-June 12

Re: Hash and List return from a subroutine of a Library.

Posted 30 June 2012 - 01:51 PM

That was actually about the only way i had not tried it. I will give it a go and see what happens, thanks.

That worked. I feel so stupid. Thanks sepp2k.
Was This Post Helpful? 0
  • +
  • -

#4 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Hash and List return from a subroutine of a Library.

Posted 30 June 2012 - 04:01 PM

View Postsepp2k, on 30 June 2012 - 09:36 PM, said:

Quote

i don't know the size of the list


You can find out the length of @nicks, by using it in a scalar context.


Also, since this is Perl, you may not even need to know the size of the list anyhow. For example, if you just want to print out all the nicks, it's just
for my $nick (@nicks) {
  print "$nick\n";
}


Or, more compactly,
print "$_\n" for @nicks;


Was This Post Helpful? 2
  • +
  • -

#5 shealyw2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-June 12

Re: Hash and List return from a subroutine of a Library.

Posted 03 July 2012 - 06:01 PM

Thanks dsherohman. I have a lot to learn :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1