On Thu, May 24, 2001 at 01:21:47PM +0100, Andy Roden wrote:
>
> Anyone help out...
>
> %router_tables is something like (1.1.1.1|cisco, 2.2.2.2|juniper etc)
>
> foreach $i (sort keys %router_tables)
> {
> next if ($i =~ "unknown");
> ($router_table{i},$router_type{$i}) = split(/\|/,
> $router_tables{i});
> if ($router_table{i} ne "") {
> print SH "\"$router_table{$i}\" ";
> }
> else {
> print SH "\"$router_tables{$i}\" ";
> }
> }
>
> For some reason, the script doesn't execute the split as I would hope
> (giving $router_table{i} = 1.1.1.1 $router_type{i} = cisco) it just
> returns two blanks (and hence prints out the $router_tables{$i} as its
> told to
You have a few mistakes here, but the biggest one is that
you're using %router_tables as a list, except that it's a hash.
What you meant to say is this:
my @router_tables = qw(1.1.1.1|cisco 2.2.2.2|juniper);
my %router_table;
my %router_type;
foreach $i (@router_tables)
{
next if ($i =~ /unknown/);
($router_table{$i},$router_type{$i}) = split(/\|/, $i);
if ($router_table{i} ne "") {
print SH "\"$router_table{$i}\" ";
}
else {
print SH "\"$i\" ";
}
}
But what you *really* meant is this:
my @router_tables = qw(1.1.1.1|cisco 2.2.2.2|juniper);
my %router = map {split("\|", $_)} @router_tables;
The keys in %router are the IP address (1.1.1.1, 2.2.2.2), and
the values in %router are the router types (cisco, juniper).
That is, $router{1.1.1.1} eq "cisco". If you're iterating over
the list of router tables, it would be something like this (I'm
not sure what this code is supposed to be doing, but here it is):
foreach my $line (@router_tables) {
next if $line =~ /unknown/;
my ($ip, $type) = split("\|", $line);
if ($ip) {
print SH '"', $ip, '" ';
} else {
print SH '"', $line, '" ';
}
}
Z.