Quantcast

General Perl syntax questions - list and hashes in methods.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

General Perl syntax questions - list and hashes in methods.

Jon Whitear
Hi folks,

I’m in the process of refactoring the Clipsal CBus code to make CBus objects more MH “native”. I’d appreciate some help with hashes and lists, which I don’t understand. The existing CBus code doesn’t use a separate package, and isn’t object oriented (don’t know if I have that bit right). It’s in cbus.pl in the common directory. It uses hashes and lists that are lexically scoped for the whole file, like so:

my @cmd_list    = ();
my %addr_not_sync    = ();

my $cmd_num = $1;
my $cmd     = $cmd_list[$cmd_num];

my @cbus_net_list = split /,/, $netlist;

I’m trying to change this into

sub new {
    my ( $class ) = @_;
    my $self = new Generic_Item();
    ...
    $$self{cmd_list}  = ();
    $$self{cbus_net_list} = ();

later on:

elsif ( $talker_msg =~ /networks=(.+)/ ) {
     my $netlist = $1;
     &::print_log ("[Clipsal CBus] Talker network list: $netlist");
     $self->{cbus_net_list} = split /,/, $netlist;          
     # Request state of network
     $self->{talker}->set ("get " . $self->{cbus_net_list}->{0} . " state”); #<- this is line 614

but I get this error:

Can't use string ("1") as a HASH ref while "strict refs" in use at /Users/jon/Documents/Development/misterhouse/mh/bin/../lib/Clipsal_CBus.pm line 614.

I’d really appreciate some guidance on how to do this!

Cheers,

Jon

------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785231&iu=/4140
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: General Perl syntax questions - list and hashes in methods.

Brian Rudy
You need to use the array element reference in [] rather than {}:

$self->{cbus_net_list}[0]

I'm not sure how tolerant split is to saving to array references rather
than arrays, but you can also try something like this for line 612:

@{$self->{cbus_net_list}} = split /,/, $netlist;


On 3/15/16 04:11, Jon Whitear wrote:

> Hi folks,
>
> I’m in the process of refactoring the Clipsal CBus code to make CBus objects more MH “native”. I’d appreciate some help with hashes and lists, which I don’t understand. The existing CBus code doesn’t use a separate package, and isn’t object oriented (don’t know if I have that bit right). It’s in cbus.pl in the common directory. It uses hashes and lists that are lexically scoped for the whole file, like so:
>
> my @cmd_list    = ();
> my %addr_not_sync    = ();
> …
> my $cmd_num = $1;
> my $cmd     = $cmd_list[$cmd_num];
>
> my @cbus_net_list = split /,/, $netlist;
>
> I’m trying to change this into
>
> sub new {
>      my ( $class ) = @_;
>      my $self = new Generic_Item();
>      ...
>      $$self{cmd_list}  = ();
>      $$self{cbus_net_list} = ();
>
> later on:
>
> elsif ( $talker_msg =~ /networks=(.+)/ ) {
>       my $netlist = $1;
>       &::print_log ("[Clipsal CBus] Talker network list: $netlist");
>       $self->{cbus_net_list} = split /,/, $netlist;
>       # Request state of network
>       $self->{talker}->set ("get " . $self->{cbus_net_list}->{0} . " state”); #<- this is line 614
>
> but I get this error:
>
> Can't use string ("1") as a HASH ref while "strict refs" in use at /Users/jon/Documents/Development/misterhouse/mh/bin/../lib/Clipsal_CBus.pm line 614.
>
> I’d really appreciate some guidance on how to do this!
>
> Cheers,
>
> Jon
>
> ------------------------------------------------------------------------------
> Transform Data into Opportunity.
> Accelerate data analysis in your applications with
> Intel Data Analytics Acceleration Library.
> Click to learn more.
> http://pubads.g.doubleclick.net/gampad/clk?id=278785231&iu=/4140
> ________________________________________________________
> To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users
>


------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785231&iu=/4140
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: General Perl syntax questions - list and hashes in methods.

Jon Whitear
Brian,

Thanks, that did the trick. It seems split doesn’t mind using a reference.

Cheers,

Jon


> On 16 Mar 2016, at 6:48 AM, Brian Rudy <[hidden email]> wrote:
>
> You need to use the array element reference in [] rather than {}:
>
> $self->{cbus_net_list}[0]
>
> I'm not sure how tolerant split is to saving to array references rather
> than arrays, but you can also try something like this for line 612:
>
> @{$self->{cbus_net_list}} = split /,/, $netlist;
>
>
> On 3/15/16 04:11, Jon Whitear wrote:
>> Hi folks,
>>
>> I’m in the process of refactoring the Clipsal CBus code to make CBus objects more MH “native”. I’d appreciate some help with hashes and lists, which I don’t understand. The existing CBus code doesn’t use a separate package, and isn’t object oriented (don’t know if I have that bit right). It’s in cbus.pl in the common directory. It uses hashes and lists that are lexically scoped for the whole file, like so:
>>
>> my @cmd_list    = ();
>> my %addr_not_sync    = ();
>> …
>> my $cmd_num = $1;
>> my $cmd     = $cmd_list[$cmd_num];
>>
>> my @cbus_net_list = split /,/, $netlist;
>>
>> I’m trying to change this into
>>
>> sub new {
>>     my ( $class ) = @_;
>>     my $self = new Generic_Item();
>>     ...
>>     $$self{cmd_list}  = ();
>>     $$self{cbus_net_list} = ();
>>
>> later on:
>>
>> elsif ( $talker_msg =~ /networks=(.+)/ ) {
>>      my $netlist = $1;
>>      &::print_log ("[Clipsal CBus] Talker network list: $netlist");
>>      $self->{cbus_net_list} = split /,/, $netlist;
>>      # Request state of network
>>      $self->{talker}->set ("get " . $self->{cbus_net_list}->{0} . " state”); #<- this is line 614
>>
>> but I get this error:
>>
>> Can't use string ("1") as a HASH ref while "strict refs" in use at /Users/jon/Documents/Development/misterhouse/mh/bin/../lib/Clipsal_CBus.pm line 614.
>>
>> I’d really appreciate some guidance on how to do this!
>>
>> Cheers,
>>
>> Jon
>>
>> ------------------------------------------------------------------------------
>> Transform Data into Opportunity.
>> Accelerate data analysis in your applications with
>> Intel Data Analytics Acceleration Library.
>> Click to learn more.
>> http://pubads.g.doubleclick.net/gampad/clk?id=278785231&iu=/4140
>> ________________________________________________________
>> To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users
>>
>
>
> ------------------------------------------------------------------------------
> Transform Data into Opportunity.
> Accelerate data analysis in your applications with
> Intel Data Analytics Acceleration Library.
> Click to learn more.
> http://pubads.g.doubleclick.net/gampad/clk?id=278785231&iu=/4140
> ________________________________________________________
> To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users
>



------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785231&iu=/4140
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users

Loading...