array not working as a hash value.

U

usaims

Hi,

I'm trying to get this little script to print:

10.230.0.14
10.230.0.15
10.230.0.16

but its printing:
1
10.230.0.ARRAY(0x8105294)
###################################

Below is the script, what am I doing wrong?

#!/usr/bin/perl
use warnings;
use strict;
my @range_one = (14, 15, 16);
my @range_two = (23, 24, 25);

my %roxiecluster = (
"1" => \@range_one,
"2" => \@range_two
);

print "What range do you want to check on?\n";
print "Press (1) and <ENTER> for Range 1
[10.230.0.14-23]\n";
print "Press (2) and <ENTER> for Range 2
[10.230.0.23-25]\n";

my $choice = <STDIN>;
chomp($choice);

my @roxie = $roxiecluster{$choice};

foreach my $x (@roxie) {
print "10.230.0.$x\n";
}
 
K

Klaus

[ snip ]
my %roxiecluster = (
"1" => \@range_one,
"2" => \@range_two
);

[ snip ]
my @roxie = $roxiecluster{$choice};

my @roxie = @{ $roxiecluster{$choice} };

That should work.

I remember vaguely that one has to use @{ ... } in those cases, but I
can't find the exact reference anymore in perldoc
 
K

Kaldrenon

I changed your code to this:

#!/usr/bin/perl
use warnings;
use strict;
$| = 1;
my @range_one = (14, 15, 16);
my @range_two = (23, 24, 25);

my %roxiecluster = (
"1" => \@range_one,
"2" => \@range_two
);

print "What range do you want to check on?\n";
print "Press (1) and <ENTER> for Range 1: [10.230.0.14-23]\n";
print "Press (2) and <ENTER> for Range 2: [10.230.0.23-25]\n";

my $choice = <STDIN>;
chomp($choice);

foreach (@{$roxiecluster{$choice}}) {
print "10.230.0.$_\n";
}

$roxiecluster{$choice} is scalar, but wrap it in @{...} and it's an
array again.

Also, I added the line $| = 1;, which causes print statements to flush
automatically. The first time I ran your version I didn't see the
prompt until I typed something in.

-Andrew
 
U

usaims

I changed your code to this:

#!/usr/bin/perl
use warnings;
use strict;
$| = 1;
my @range_one = (14, 15, 16);
my @range_two = (23, 24, 25);

my %roxiecluster = (
"1" => \@range_one,
"2" => \@range_two
);

print "What range do you want to check on?\n";
print "Press (1) and <ENTER> for Range 1: [10.230.0.14-23]\n";
print "Press (2) and <ENTER> for Range 2: [10.230.0.23-25]\n";

my $choice = <STDIN>;
chomp($choice);

foreach (@{$roxiecluster{$choice}}) {
print "10.230.0.$_\n";

}

$roxiecluster{$choice} is scalar, but wrap it in @{...} and it's an
array again.

Also, I added the line $| = 1;, which causes print statements to flush
automatically. The first time I ran your version I didn't see the
prompt until I typed something in.

-Andrew

Thank you all

my @roxie = @{ $roxiecluster{$choice} }; DID THE TRICK :) !
 
M

Michele Dondi

Subject: array not working as a hash value. [snip]
but its printing:
1
10.230.0.ARRAY(0x8105294)

You forgot dereferencing. See

perldoc perlref

for instructions about how to do so.


Michele
 
P

Paul Lalli

my @roxie = @{ $roxiecluster{$choice} };

That should work.

I remember vaguely that one has to use @{ ... } in those
cases, but I can't find the exact reference anymore in perldoc

How ironic... :)

perldoc perlref
"ref", for "references", you see.

Paul Lalli
 
D

Dr.Ruud

usaims schreef:
my @range_one = (14, 15, 16);
my @range_two = (23, 24, 25);

If you number your variables, strongly consider arrays.

For example (mixing several Perl-styles):

#!/usr/bin/perl
use warnings;
use strict;

my $base = "10.230.0.";

my @ranges = (
[14..16],
[23..25],
[0..7],
);

print "What range do you want to check on?\n";

my $choice;

for my $r ( 0 .. $#ranges ) {
my $n = $ranges[ $r ];
$choice = $r + 1;
print "Press ($choice) and <ENTER> for Range $choice
[$base$n->[0]-$n->[-1]]\n";
}

$choice = 0 + <STDIN>;
$choice >= 1 and $choice <= @ranges
or die "Wrong Range: $choice\n";

print "$base$_\n" for @{$ranges[ $choice - 1 ]};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top