Reference to hash and array

Discussion in 'Perl Misc' started by Vito Corleone, Dec 2, 2004.

  1. 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
     
    Vito Corleone, Dec 2, 2004
    #1
    1. Advertising

  2. Vito Corleone

    Anno Siegel Guest

    Vito Corleone <> 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
     
    Anno Siegel, Dec 2, 2004
    #2
    1. Advertising

  3. Vito Corleone

    KKramsch Guest

    In <> Vito Corleone <> 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 ip:port" or "ip:port,num"
    my ($ip_port, $num) = split /,/, $info;
    if ($num) {
    # $info is of the form "ip:port,num"
    push @servers, [$ip_port, $num];
    }
    else {
    # info is of the form "ip:port"
    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.
     
    KKramsch, Dec 2, 2004
    #3
  4. Ok, I got it now. Thanks Karl :)

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

    > In <> Vito Corleone <> 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 ip:port" or "ip:port,num"
    > my ($ip_port, $num) = split /,/, $info;
    > if ($num) {
    > # $info is of the form "ip:port,num"
    > push @servers, [$ip_port, $num];
    > }
    > else {
    > # info is of the form "ip:port"
    > 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.
     
    Vito Corleone, Dec 3, 2004
    #4
  5. Vito Corleone

    KKramsch Guest

    In <> Vito Corleone <> 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 <> wrote:


    >> In <> Vito Corleone <> 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 ip:port" or "ip:port,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 "ip:port,num"
    >> push @servers, [$ip_port, $num];
    >> }
    >> else {
    >> # info is of the form "ip:port"
    >> 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.
     
    KKramsch, Dec 3, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    534
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    275
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    233
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    624
    David A. Black
    Jul 2, 2008
  5. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    120
    Steve
    Sep 28, 2003
Loading...

Share This Page