D
doni
Hi,
I am having some problem with ARRAY ref in my test program.
The test program contains multiple arrays to extract the data from the
output based upon certain conditions. I am also extracting an user
input to a variable where the user is expected to input any one of the
array names existing
in the program.
When I am trying to pass the user input variable to a subroutine as an
initialized array, I am getting this error message: Can't use string
("junk") as an ARRAY ref while "strict refs".
Actually, what I am expecting to do is have the user input an already
existing array name in the program and pass that array name to a
subroutine.
Here is the program:
#! /usr/bin/perl
use strict;
my $ex_data = 'log_messages';
open (NETSTAT,$ex_data) || die("Cannot Open File: $!");
### Variables ###
my %gw_route = ();
my %route = ();
my %no_of_times = ();
my $segment;
my @new_device; my @route_update;
my @received_hello; my @removing_l2;
my $key; my $i;
### User Input
$segment = <STDIN>;
chomp($segment);
### ROUTE SUBROUTINE ###
sub route {
my (@received_message) = @_;
@received_message = split;
if (/Received Hello/i) {
push (@received_hello, $received_message[9]);
}
elsif (/Removing L2/) {
push (@removing_l2, $received_message[9]);
push @{$route{removing_l2}{$key}}, $received_message[9];
}
else {}
}
### GWD SUBROUTINE ###
sub gw_route {
my (@received_message) = @_;
@received_message = split;
if (/Got ROUTE_UPDATE/i) {
(my $gw_route_update = $received_message[7]) =~ s/address=/ /;
push (@route_update, $gw_route_update);
push @{$gw_route{route_update}{$key}}, $gw_route_update;
}
}
### SUBROUTINE TO COUNT NO OF TIMES A PARTICULAR VALUE IS PRESENT IN
THE HASH
sub no_of_times {
my (@mac_id) = @_;
for $key (@mac_id) {
$no_of_times{$key}++;
}
for $key (keys %no_of_times) {
print "$no_of_times{$key} $key\n";
}
}
while (<NETSTAT>) {
chomp;
if (/rfrouted/i) {
route($_);
}
elsif (/gwd/i) {
gw_route($_);
}
}
close(NETSTAT) || die("Cannot close $ex_data: $!");
no_of_times(@$segment); #### This is where I am creating an error
Thanks,
doni
I am having some problem with ARRAY ref in my test program.
The test program contains multiple arrays to extract the data from the
output based upon certain conditions. I am also extracting an user
input to a variable where the user is expected to input any one of the
array names existing
in the program.
When I am trying to pass the user input variable to a subroutine as an
initialized array, I am getting this error message: Can't use string
("junk") as an ARRAY ref while "strict refs".
Actually, what I am expecting to do is have the user input an already
existing array name in the program and pass that array name to a
subroutine.
Here is the program:
#! /usr/bin/perl
use strict;
my $ex_data = 'log_messages';
open (NETSTAT,$ex_data) || die("Cannot Open File: $!");
### Variables ###
my %gw_route = ();
my %route = ();
my %no_of_times = ();
my $segment;
my @new_device; my @route_update;
my @received_hello; my @removing_l2;
my $key; my $i;
### User Input
$segment = <STDIN>;
chomp($segment);
### ROUTE SUBROUTINE ###
sub route {
my (@received_message) = @_;
@received_message = split;
if (/Received Hello/i) {
push (@received_hello, $received_message[9]);
}
elsif (/Removing L2/) {
push (@removing_l2, $received_message[9]);
push @{$route{removing_l2}{$key}}, $received_message[9];
}
else {}
}
### GWD SUBROUTINE ###
sub gw_route {
my (@received_message) = @_;
@received_message = split;
if (/Got ROUTE_UPDATE/i) {
(my $gw_route_update = $received_message[7]) =~ s/address=/ /;
push (@route_update, $gw_route_update);
push @{$gw_route{route_update}{$key}}, $gw_route_update;
}
}
### SUBROUTINE TO COUNT NO OF TIMES A PARTICULAR VALUE IS PRESENT IN
THE HASH
sub no_of_times {
my (@mac_id) = @_;
for $key (@mac_id) {
$no_of_times{$key}++;
}
for $key (keys %no_of_times) {
print "$no_of_times{$key} $key\n";
}
}
while (<NETSTAT>) {
chomp;
if (/rfrouted/i) {
route($_);
}
elsif (/gwd/i) {
gw_route($_);
}
}
close(NETSTAT) || die("Cannot close $ex_data: $!");
no_of_times(@$segment); #### This is where I am creating an error
Thanks,
doni