Reputation:
I am trying to execute the below mentioned code and observed the error
Can't use string ("RCSoWLAN;ePDG-2;Qguest;ASUS_ATT_"...) as an ARRAY ref while "strict refs" in use.
#!/perl64/bin/perl
use strict;
use warnings;
my $result_string = 'RCSoWLAN;ePDG-2;Qguest;ASUS_ATT_VOWIFI;RCS_IWLAN;Qguest;Pandora;Hydra;ASUS_ATT_VOWIFI_5G;Linksys_Performance_2.4G;RCS_NAT;ePDG7;Qguest;Pandora;Hydra;ipv6testap;Linksys_Performance_5G;ASUS_stress_5G;Hydra';
my $index = 1;
foreach ( @{ $result_string } ) {
print "SSID[$index]: $_->{$index}->{ssid}\n";
$index++;
}
Upvotes: 2
Views: 3991
Reputation: 241908
$result_string is initialized with a string value. You cannot dereference a string value as an array reference.
If you want to split the string on semicolons, use split:
my @results = split /;/, $result_string;
You can then iterate over @results
:
for my $result (@results) { ...
You haven't explained how the structure should be populated from the string, so I can't help you with the rest of the code.
Upvotes: 4
Reputation: 126722
As the error message says, @{ $result_string }
tries to dereference the string as if it were an array reference. But it's just a string, so Perl can't do it for you
It looks as though you have semicolon-separated data, and the easiest approach is to use split
to separate it into fields
This should work better for you
for ( split /;/, $result_string ) {
print "SSID[$index]: $_\n";
++$index;
}
but I can't follow what you're trying to do with $_->{$index}->{ssid}
. Perhaps you would explain?
Upvotes: 5