2

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.

Perl code

#!/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++;
}
Community
  • 1
  • 1

2 Answers2

5

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?

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • Hi, Thanks for your help!! I am the beginner in PERL coding and facing below error this time after making the changes that you suggested: Can't use string ("RCSoWLAN") as a HASH ref while "strict refs" in use #!/perl64/bin/perl use strict; use warnings; my $result_string= 'RCSoWLAN;ePDG-2;Qguest;ASUS_ATT_VOWIFI'; my $index = 1; for ( split /;/, $result_string ) { print "SSID[$index]: $_->{$index}->{ssid}\n"; $index++; } –  May 18 '18 at 06:24
4

$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.

choroba
  • 231,213
  • 25
  • 204
  • 289