Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Reference to hash and array

Reply
Thread Tools

Reference to hash and array

 
 
Vito Corleone
Guest
Posts: n/a
 
      12-02-2004
Hi,
I downloaded a module that need me to input the value in very confusing
way. For example:
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
'debug' => 0 } );

You put the list of your servers IP and port number into servers. I put
my servers list in config file. It looks like this:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';

And then when I load the module, I do:
my @servers = split(" ", SERVERS);
$m = Module::->new(
{ 'servers' => \@servers,
'debug' => 0 } );

So far so good. But you can also load the module this way.
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
'debug' => 0 } );

Besides IP and port number, it also takes the value of the server (ie:
3). I want to make my config file looks like:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';

But I don't know how can I pass these values to the module. Or is there
any better way to keep these values in config file? Please help, and
thanks in advance.

--vc

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      12-02-2004
Vito Corleone <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi,
> I downloaded a module that need me to input the value in very confusing
> way. For example:
> $m = Module::->new(
> { 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
> 'debug' => 0 } );
>
> You put the list of your servers IP and port number into servers. I put
> my servers list in config file. It looks like this:
> use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';
>
> And then when I load the module, I do:
> my @servers = split(" ", SERVERS);
> $m = Module::->new(
> { 'servers' => \@servers,
> 'debug' => 0 } );
>
> So far so good. But you can also load the module this way.
> $m = Module::->new(
> { 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
> 'debug' => 0 } );
>
> Besides IP and port number, it also takes the value of the server (ie:
> 3). I want to make my config file looks like:
> use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';
>
> But I don't know how can I pass these values to the module. Or is there
> any better way to keep these values in config file? Please help, and
> thanks in advance.


Untested:

my @servers = map /,/ ? [ split /,/] : $_, split ' ', SERVERS;

Anno
 
Reply With Quote
 
 
 
 
KKramsch
Guest
Posts: n/a
 
      12-02-2004
In <(E-Mail Removed)> Vito Corleone <(E-Mail Removed)> writes:

>Hi,
>I downloaded a module that need me to input the value in very confusing
>way. For example:
>$m = Module::->new(
> { 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
> 'debug' => 0 } );


>You put the list of your servers IP and port number into servers. I put
>my servers list in config file. It looks like this:
>use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';


>And then when I load the module, I do:
>my @servers = split(" ", SERVERS);
>$m = Module::->new(
> { 'servers' => \@servers,
> 'debug' => 0 } );


>So far so good. But you can also load the module this way.
>$m = Module::->new(
> { 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
> 'debug' => 0 } );


>Besides IP and port number, it also takes the value of the server (ie:
>3). I want to make my config file looks like:
>use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';


>But I don't know how can I pass these values to the module. Or is there
>any better way to keep these values in config file? Please help, and
>thanks in advance.


What Anno posted is right, I'm sure, but the code below may be
easier to follow.

# untested
my @servers;
for my $info (split ' ', SERVERS) {
# $info is either of the form iport" or "iport,num"
my ($ip_port, $num) = split /,/, $info;
if ($num) {
# $info is of the form "iport,num"
push @servers, [$ip_port, $num];
}
else {
# info is of the form "iport"
push @servers, $info;
}
}

my $m = Module::->new( { 'servers' => \@servers,
'debug' => 0 } );

__END__

Makes sense?

Karl

--
Sent from a spam-bucket account; I check it once in a blue moon. If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.
 
Reply With Quote
 
Vito Corleone
Guest
Posts: n/a
 
      12-03-2004
Ok, I got it now. Thanks Karl

On Thu, 2 Dec 2004 16:44:11 +0000 (UTC)
KKramsch <(E-Mail Removed)> wrote:

> In <(E-Mail Removed)> Vito Corleone <(E-Mail Removed)> writes:
>
> >Hi,
> >I downloaded a module that need me to input the value in very confusing
> >way. For example:
> >$m = Module::->new(
> > { 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
> > 'debug' => 0 } );

>
> >You put the list of your servers IP and port number into servers. I put
> >my servers list in config file. It looks like this:
> >use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';

>
> >And then when I load the module, I do:
> >my @servers = split(" ", SERVERS);
> >$m = Module::->new(
> > { 'servers' => \@servers,
> > 'debug' => 0 } );

>
> >So far so good. But you can also load the module this way.
> >$m = Module::->new(
> > { 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
> > 'debug' => 0 } );

>
> >Besides IP and port number, it also takes the value of the server (ie:
> >3). I want to make my config file looks like:
> >use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';

>
> >But I don't know how can I pass these values to the module. Or is there
> >any better way to keep these values in config file? Please help, and
> >thanks in advance.

>
> What Anno posted is right, I'm sure, but the code below may be
> easier to follow.
>
> # untested
> my @servers;
> for my $info (split ' ', SERVERS) {
> # $info is either of the form iport" or "iport,num"
> my ($ip_port, $num) = split /,/, $info;
> if ($num) {
> # $info is of the form "iport,num"
> push @servers, [$ip_port, $num];
> }
> else {
> # info is of the form "iport"
> push @servers, $info;
> }
> }
>
> my $m = Module::->new( { 'servers' => \@servers,
> 'debug' => 0 } );
>
> __END__
>
> Makes sense?
>
> Karl
>
> --
> Sent from a spam-bucket account; I check it once in a blue moon. If
> you still want to e-mail me, cut out the extension from my address,
> and make the obvious substitutions on what's left.

 
Reply With Quote
 
KKramsch
Guest
Posts: n/a
 
      12-03-2004
In <(E-Mail Removed)> Vito Corleone <(E-Mail Removed)> writes:

>Ok, I got it now. Thanks Karl


Actually, I spotted a bug. See below.

>On Thu, 2 Dec 2004 16:44:11 +0000 (UTC)
>KKramsch <(E-Mail Removed)> wrote:


>> In <(E-Mail Removed)> Vito Corleone <(E-Mail Removed)> writes:
>>
>> >Hi,
>> >I downloaded a module that need me to input the value in very confusing
>> >way. For example:
>> >$m = Module::->new(
>> > { 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
>> > 'debug' => 0 } );

>>
>> >You put the list of your servers IP and port number into servers. I put
>> >my servers list in config file. It looks like this:
>> >use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';

>>
>> >And then when I load the module, I do:
>> >my @servers = split(" ", SERVERS);
>> >$m = Module::->new(
>> > { 'servers' => \@servers,
>> > 'debug' => 0 } );

>>
>> >So far so good. But you can also load the module this way.
>> >$m = Module::->new(
>> > { 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
>> > 'debug' => 0 } );

>>
>> >Besides IP and port number, it also takes the value of the server (ie:
>> >3). I want to make my config file looks like:
>> >use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';

>>
>> >But I don't know how can I pass these values to the module. Or is there
>> >any better way to keep these values in config file? Please help, and
>> >thanks in advance.

>>
>> What Anno posted is right, I'm sure, but the code below may be
>> easier to follow.
>>
>> # untested
>> my @servers;
>> for my $info (split ' ', SERVERS) {
>> # $info is either of the form iport" or "iport,num"
>> my ($ip_port, $num) = split /,/, $info;
>> if ($num) {

^^^^^^^^^

That last line should be

if (defined $num) {

Otherwise you'd get incorrect results if $num happened to be 0
(assuming that such value makes sense).

Karl

>> # $info is of the form "iport,num"
>> push @servers, [$ip_port, $num];
>> }
>> else {
>> # info is of the form "iport"
>> push @servers, $info;
>> }
>> }
>>
>> my $m = Module::->new( { 'servers' => \@servers,
>> 'debug' => 0 } );
>>
>> __END__
>>
>> Makes sense?
>>
>> Karl
>>
>> --
>> Sent from a spam-bucket account; I check it once in a blue moon. If
>> you still want to e-mail me, cut out the extension from my address,
>> and make the obvious substitutions on what's left.

--
Sent from a spam-bucket account; I check it once in a blue moon. If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
hash reference as a hash key Steve Perl Misc 6 09-28-2003 10:49 AM



Advertisments