assigning hash key to reference of array question

--20cf30050d983bcbdf049d5cf3d6
Content-Type: text/plain; charset=ISO-8859-1

I have a below program and I am not doing it right.
Currently, only last ip pool is going in since I am putting them w/ key to
values(so only last one shows up when I print).

How can I aggregate and assign them to server_1 so that when I print below
will show up?

server_1
10.1.1.1
10.1.1.2
10.1.1.3
10.1.1.4
10.1.1.5
192.168.1.1
192.168.1.2


while ( <DATA> ) {
my [at] array_ip;
chomp;
my ($swit,$server,$ip_range) = split;

my ($b_real_ip,$b_ip, $e_ip) = $ip_range =~
m#(\d+\.\d+\.\d+\.)(\d+)\-(\d+)#;
my $b_ip_copy = $b_ip;
my [at] ip_g;
while ( $b_ip_copy <= $e_ip ) {
my $b_ip_final = join('',$b_real_ip,$b_ip_copy);
push [at] ip_g , $b_ip_final;
++$b_ip_copy;
}
$HoA{ join('_',$swit,$server) } = \ [at] ip_g;
}


__DATA__
server 1 10.1.1.1-10.1.1.5
server 2 192.168.1.1-192.168.1.5
server 1 192.168.1.1-192.168.1.2

--20cf30050d983bcbdf049d5cf3d6--
Richard Lee [ Mo, 28 Februar 2011 20:47 ] [ ID #2055948 ]

Re: assigning hash key to reference of array question

--0016e68cc10a16f3ea049d5d13b7
Content-Type: text/plain; charset=ISO-8859-1

I am not sure if I am still in mailing list. so cc'ing myself.

On Mon, Feb 28, 2011 at 2:47 PM, steve park <rich.japh [at] gmail.com> wrote:

>
> I have a below program and I am not doing it right.
> Currently, only last ip pool is going in since I am putting them w/ key to
> values(so only last one shows up when I print).
>
> How can I aggregate and assign them to server_1 so that when I print below
> will show up?
>
> server_1
> 10.1.1.1
> 10.1.1.2
> 10.1.1.3
> 10.1.1.4
> 10.1.1.5
> 192.168.1.1
> 192.168.1.2
>
>
> while ( <DATA> ) {
> my [at] array_ip;
> chomp;
> my ($swit,$server,$ip_range) = split;
>
> my ($b_real_ip,$b_ip, $e_ip) = $ip_range =~
> m#(\d+\.\d+\.\d+\.)(\d+)\-(\d+)#;
> my $b_ip_copy = $b_ip;
> my [at] ip_g;
> while ( $b_ip_copy <= $e_ip ) {
> my $b_ip_final = join('',$b_real_ip,$b_ip_copy);
> push [at] ip_g , $b_ip_final;
> ++$b_ip_copy;
> }
> $HoA{ join('_',$swit,$server) } = \ [at] ip_g;
> }
>
>
> __DATA__
> server 1 10.1.1.1-10.1.1.5
> server 2 192.168.1.1-192.168.1.5
> server 1 192.168.1.1-192.168.1.2
>
>
>

--0016e68cc10a16f3ea049d5d13b7--
Richard Lee [ Mo, 28 Februar 2011 20:56 ] [ ID #2055949 ]

Re: assigning hash key to reference of array question

Hi Steve,

On Monday 28 Feb 2011 21:47:30 steve park wrote:
> I have a below program and I am not doing it right.

Is this the complete program?

> Currently, only last ip pool is going in since I am putting them w/ key to
> values(so only last one shows up when I print).

OK. Have you localised a variable to its innermost scope?

>
> How can I aggregate and assign them to server_1 so that when I print below
> will show up?
>
> server_1
> 10.1.1.1
> 10.1.1.2
> 10.1.1.3
> 10.1.1.4
> 10.1.1.5
> 192.168.1.1
> 192.168.1.2

Use a hash or an array reference.

More comments on your code, below:

>
>
> while ( <DATA> ) {

do =ABwhile ( my $line =3D <DATA> ) {=BB and use =AB$line=BB instead.

> my [at] array_ip;

> chomp;
> my ($swit,$server,$ip_range) =3D split;
>
> my ($b_real_ip,$b_ip, $e_ip) =3D $ip_range =3D~
> m#(\d+\.\d+\.\d+\.)(\d+)\-(\d+)#;
> my $b_ip_copy =3D $b_ip;
> my [at] ip_g;
> while ( $b_ip_copy <=3D $e_ip ) {
> my $b_ip_final =3D join('',$b_real_ip,$b_ip_copy);
> push [at] ip_g , $b_ip_final;
> ++$b_ip_copy;
> }

This should be a for loop, or maybe a map.

> $HoA{ join('_',$swit,$server) } =3D \ [at] ip_g;

join('_',$swit,$server) is a complicated way to say:
=AB"${swit}_$server"=BB.

Regards,

Shlomi Fish

=2D-
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
http://www.shlomifish.org/humour/ways_to_do_it.html

An apple a day keeps the doctor away.
Two apples a day will keep two doctors away. (-- one of Shlomi Fish's
relatives)

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Mo, 28 Februar 2011 21:46 ] [ ID #2055951 ]

Re: assigning hash key to reference of array question

On Mon, Feb 28, 2011 at 02:47:30PM -0500, steve park wrote:
Hello,

A couple of things in addition to what Shlomi had already mentioned.
First, you must check your regex. It doesn't really match what you have
mentioned in the __DATA__ section.

Next, doing join('', ...) is just a verbose way of using the string concat
operator ".". Like so, "ABCDEF" is equivalent to "ABC"."DEF".

Third, (a minor thing) are you aware that what you have in the __DATA__ actually
leads to overwriting your values in the hash?
>
> while ( <DATA> ) {
> my [at] array_ip;
> chomp;
> my ($swit,$server,$ip_range) = split;
>
> my ($b_real_ip,$b_ip, $e_ip) = $ip_range =~
> m#(\d+\.\d+\.\d+\.)(\d+)\-(\d+)#;
> my $b_ip_copy = $b_ip;
> my [at] ip_g;
> while ( $b_ip_copy <= $e_ip ) {
> my $b_ip_final = join('',$b_real_ip,$b_ip_copy);
> push [at] ip_g , $b_ip_final;
> ++$b_ip_copy;
> }
> $HoA{ join('_',$swit,$server) } = \ [at] ip_g;
> }
>
>
> __DATA__
> server 1 10.1.1.1-10.1.1.5
> server 2 192.168.1.1-192.168.1.5
> server 1 192.168.1.1-192.168.1.2

HTH,

Shrivats

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shrivats [ Mo, 28 Februar 2011 22:15 ] [ ID #2055952 ]

Re: assigning hash key to reference of array question

On 2/28/11 Mon Feb 28, 2011 11:47 AM, "steve park" <rich.japh [at] gmail.com>
scribbled:

> I have a below program and I am not doing it right.
> Currently, only last ip pool is going in since I am putting them w/ key to
> values(so only last one shows up when I print).
>
> How can I aggregate and assign them to server_1 so that when I print below
> will show up?
>
> server_1
> 10.1.1.1
> 10.1.1.2
> 10.1.1.3
> 10.1.1.4
> 10.1.1.5
> 192.168.1.1
> 192.168.1.2
>
>

Presumably you have the following line before the loop:

my $HoA;

> while ( <DATA> ) {
> my [at] array_ip;
> chomp;
> my ($swit,$server,$ip_range) = split;
>
> my ($b_real_ip,$b_ip, $e_ip) = $ip_range =~
> m#(\d+\.\d+\.\d+\.)(\d+)\-(\d+)#;
> my $b_ip_copy = $b_ip;
> my [at] ip_g;

Since you are declaring [at] ip_g inside the while loop, it gets redefined to an
empty array for each iteration. Therefore, it will never contain values from
more than one line.

> while ( $b_ip_copy <= $e_ip ) {
> my $b_ip_final = join('',$b_real_ip,$b_ip_copy);
> push [at] ip_g , $b_ip_final;

Substitute the following line (untested) for the above:

push( [at] {$HoA{ join('_',$swit,$server) }}, $b_ip_final );

> ++$b_ip_copy;
> }
> $HoA{ join('_',$swit,$server) } = \ [at] ip_g;

And delete the above line.

> }
>
>
> __DATA__
> server 1 10.1.1.1-10.1.1.5
> server 2 192.168.1.1-192.168.1.5
> server 1 192.168.1.1-192.168.1.2

Also, "${swit}_${server}" might be easier to understand than
join('_',$swit,$server).




--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Mo, 28 Februar 2011 22:20 ] [ ID #2055953 ]

Re: assigning hash key to reference of array question

On 28/02/2011 19:56, steve park wrote:
>
> I have a below program and I am not doing it right. Currently, only
> last ip pool is going in since I am putting them w/ key to values(so
> only last one shows up when I print).
>
> How can I aggregate and assign them to server_1 so that when I print
> below will show up?
>
> server_1
> 10.1.1.1
> 10.1.1.2
> 10.1.1.3
> 10.1.1.4
> 10.1.1.5
> 192.168.1.1
> 192.168.1.2
>
>
> while (<DATA> ) {
> my [at] array_ip;

This array isn't used.

> chomp;
> my ($swit,$server,$ip_range) = split;

There's no need to chomp as you are splitting on whitespace anyway.

> my ($b_real_ip,$b_ip, $e_ip) = $ip_range =~ m#(\d+\.\d+\.\d+\.)(\d+)\-(\d+)#;

You aren't grabbing the correct part of the 'end' quartet - $e_ip is
being set to the first byte of the four instead of the last. For
instance, for the line

server 2 192.168.1.1-192.168.1.5

$e_ip takes the value 192 instead of 5. I suggest you change this to

my ($b_real_ip,$b_ip, $e_ip) = $ip_range =~ /(\d+\.\d+\.\d+\.)(\d+)-\1(\d+)/;

which will match a second copy of the first three bytes, and then
capture the final byte.

> my $b_ip_copy = $b_ip;
> my [at] ip_g;

If you push directly onto the arrays in the hash you will get the result
you want and won't need this secondary array.

> while ( $b_ip_copy<= $e_ip ) {
> my $b_ip_final = join('',$b_real_ip,$b_ip_copy);
> push [at] ip_g , $b_ip_final;
> ++$b_ip_copy;
> }
> $HoA{ join('_',$swit,$server) } = \ [at] ip_g;

The last loop is better written

for my $ip ($b_ip .. $e_ip) {
push [at] {$HoA{$swit.'_'.$server}}, $b_real_ip.$ip;
}

> }
>
>
> __DATA__
> server 1 10.1.1.1-10.1.1.5
> server 2 192.168.1.1-192.168.1.5
> server 1 192.168.1.1-192.168.1.2

The complete program is below.

HTH,

Rob

use strict;
use warnings;

my %HoA;

while ( <DATA> ) {

my ($swit, $server, $ip_range) = split;

my ($b_real_ip, $b_ip, $e_ip) = $ip_range =~ /(\d+\.\d+\.\d+\.)(\d+)-\1(\d+)/;

for my $byte ($b_ip .. $e_ip) {
push [at] {$HoA{$swit.'_'.$server}}, $b_real_ip.$byte;
}
}

__DATA__
server 1 10.1.1.1-10.1.1.5
server 2 192.168.1.1-192.168.1.5
server 1 192.168.1.1-192.168.1.2

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Mo, 28 Februar 2011 23:33 ] [ ID #2055954 ]

Re: assigning hash key to reference of array question

On Feb 28, 2:47=A0pm, rich.j... [at] gmail.com (steve park) wrote:
> I have a below program and I am not doing it right.
> Currently, only last ip pool is going in since I am putting them w/ key t=
o
> values(so only last one shows up when I print).

Hello Steve,

The reason you only get the last value for the 2 'server_1' keys is
because the last value to be parsed and stored in the hash your way
overwrites any previous value you would have stored under that key.
You could fix this by pushing onto an array for each uniquie key.
Notice my added line of code showing how to do it (below).


>
> How can I aggregate and assign them to server_1 so that when I print belo=
w
> will show up?
>
> server_1
> 10.1.1.1
> 10.1.1.2
> 10.1.1.3
> 10.1.1.4
> 10.1.1.5
> 192.168.1.1
> 192.168.1.2
>
> while ( <DATA> ) {
> =A0 =A0 =A0 =A0my [at] array_ip;
> =A0 =A0 =A0 =A0chomp;
> =A0 =A0 =A0 =A0my ($swit,$server,$ip_range) =3D split;
>
> =A0 =A0 =A0 =A0my ($b_real_ip,$b_ip, $e_ip) =3D $ip_range =3D~
> m#(\d+\.\d+\.\d+\.)(\d+)\-(\d+)#;
> =A0 =A0 =A0 =A0my $b_ip_copy =3D $b_ip;
> =A0 =A0 =A0 =A0my [at] ip_g;
> =A0 =A0 =A0 =A0while ( $b_ip_copy <=3D $e_ip ) {
> =A0 =A0 =A0 =A0 =A0 =A0my $b_ip_final =3D join('',$b_real_ip,$b_ip_copy);
> =A0 =A0 =A0 =A0 =A0 =A0push [at] ip_g , $b_ip_final;
> =A0 =A0 =A0 =A0 =A0 =A0++$b_ip_copy;
> =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0$HoA{ join('_',$swit,$server) } =3D \ [at] ip_g;

push [at] { $HoA{ join('_',$swit,$server) }}, \ [at] ip_g


>
> }
>
> __DATA__
> server =A0 =A01 =A0 =A010.1.1.1-10.1.1.5
> server =A0 =A02 =A0 =A0192.168.1.1-192.168.1.5
> server =A0 =A01 =A0 =A0192.168.1.1-192.168.1.2

Here I used a recent (and handy) module, Data::IPV4::Range::Parse,
when working with IPV4 strings.

#!/usr/bin/perl
use strict;
use warnings;
use 5.012;
use Data::Dumper;
use Data::IPV4::Range::Parse qw/auto_parse_ipv4_range int_to_ip/;

my %data;

while ( <DATA> ) {
my ($swit,$server,$ip_range) =3D split;
my ($start_int,$end_int)=3Dauto_parse_ipv4_range($ip_range);
my [at] ip =3D map int_to_ip( $_ ), $start_int .. $end_int;

push [at] { $data{ "${swit}_$server" }}, [ [at] ip];
}

print Dumper \%data;

__DATA__
server 1 10.1.1.1-10.1.1.5
server 2 192.168.1.1-192.168.1.5
server 1 192.168.1.1-192.168.1.2

** prints

C:\Old_Data\perlp>perl t6.pl
$VAR1 =3D {
'server_1' =3D> [
[
'10.1.1.1',
'10.1.1.2',
'10.1.1.3',
'10.1.1.4',
'10.1.1.5'
],
[
'192.168.1.1',
'192.168.1.2'
]
],
'server_2' =3D> [
[
'192.168.1.1',
'192.168.1.2',
'192.168.1.3',
'192.168.1.4',
'192.168.1.5'
]
]
};


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
charley [ Di, 01 März 2011 00:02 ] [ ID #2055955 ]

Re: assigning hash key to reference of array question

At 22:33 +0000 28/02/2011, Rob Dixon wrote:

>The complete program is below.
>
>HTH,
>
>Rob
>
>use strict;
>use warnings;
>
>my %HoA;
>
>while ( <DATA> ) {
>
> my ($swit, $server, $ip_range) = split;
>
> my ($b_real_ip, $b_ip, $e_ip) = $ip_range =~
>/(\d+\.\d+\.\d+\.)(\d+)-\1(\d+)/;
>
> for my $byte ($b_ip .. $e_ip) {
> push [at] {$HoA{$swit.'_'.$server}}, $b_real_ip.$byte;
> }
>}
>
>__DATA__
>server 1 10.1.1.1-10.1.1.5
>server 2 192.168.1.1-192.168.1.5
>server 1 192.168.1.1-192.168.1.2

and then:


for(sort keys %HoA){
print "$_\n";
for ( [at] {$HoA{$_}}){
print "$_\n";
}
}

#JD

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
John Delacour [ Di, 01 März 2011 00:44 ] [ ID #2056007 ]
Perl » gmane.comp.lang.perl.beginners » assigning hash key to reference of array question

Vorheriges Thema: Creating PDF/FDF files
Nächstes Thema: Help on a socket problem