Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Printing of Array Hash is Missing Elements

by spickles (Scribe)
on Sep 28, 2009 at 20:25 UTC ( [id://797996]=note: print w/replies, xml ) Need Help??


in reply to Re: Printing of Array Hash is Missing Elements
in thread Printing of Array Hash is Missing Elements

Grandfather -

Ok, so I modified some of the data file, as well as the suggestions already posted. It is clear, now, after modifying the building names that the output is only containing unique records. The input is this:

VLAN Tag 1st Octet 2nd Octet 3rd Octet 4th Octet Mask + Mask Subnet Building Description 321 10 32 32 0 19 255.255.224.0 10.32.32.0 /19 + Building1 Data VLAN 322 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding1 Data VLAN 323 10 32 128 0 19 255.255.224.0 10.32.128.0 /19 + Building1 Data VLAN 324 10 32 96 0 19 255.255.224.0 10.32.96.0 /19 + Building1 Data VLAN 325 10 32 160 0 19 255.255.224.0 10.32.160.0 /19 + Building1 Data VLAN 326 10 32 192 0 19 255.255.224.0 10.32.192.0 /19 + Building1 Data VLAN 327 10 32 64 0 19 255.255.224.0 10.32.64.0 /19 + Building1 Data VLAN 328 10 32 248 0 22 255.255.252.0 10.32.248.0 /22 + Building1 Data VLAN 329 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding1 Data VLAN 330 10 32 224 0 24 255.255.255.0 10.32.224.0 /24 + Building1 Data VLAN 331 10 32 225 0 24 255.255.255.0 10.32.225.0 /24 + Building2 Data VLAN 332 10 32 228 0 24 255.255.255.0 10.32.228.0 /24 + Building2 Data VLAN 333 10 32 229 0 24 255.255.255.0 10.32.229.0 /24 + Building2 Data VLAN 334 10 32 232 0 24 255.255.255.0 10.32.232.0 /24 + Building2 Data VLAN 335 10 32 233 0 24 255.255.255.0 10.32.233.0 /24 + Building2 Data VLAN 336 10 32 236 0 24 255.255.255.0 10.32.236.0 /24 + Building2 Data VLAN 337 10 32 237 0 24 255.255.255.0 10.32.237.0 /24 + Building2 Data VLAN 338 10 32 240 0 24 255.255.255.0 10.32.240.0 /24 + Building2 Data VLAN 339 10 32 241 0 24 255.255.255.0 10.32.241.0 /24 + Building2 Data VLAN 340 10 32 244 0 24 255.255.255.0 10.32.244.0 /24 + Building2 Data VLAN 341 10 32 245 0 24 255.255.255.0 10.32.245.0 /24 + Building3 Data VLAN 342 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding3 Data VLAN 343 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding3 Data VLAN 344 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding3 Data VLAN 345 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding3 Data VLAN 346 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding3 Data VLAN 347 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding3 Data VLAN 348 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding3 Data VLAN 349 10 32 0 24 255.255.255.0 10.32..0 /24 Bui +lding3 Data VLAN 350 10 32 2 0 23 255.255.254.0 10.32.2.0 /23 B +uilding3 Data VLAN 351 10 32 4 0 23 255.255.254.0 10.32.4.0 /23 B +uilding4 Data VLAN 352 10 32 6 0 23 255.255.254.0 10.32.6.0 /23 B +uilding4 Data VLAN 353 10 32 8 0 23 255.255.254.0 10.32.8.0 /23 B +uilding4 Data VLAN 354 10 32 10 0 23 255.255.254.0 10.32.10.0 /23 + Building4 Data VLAN 355 10 32 12 0 23 255.255.254.0 10.32.12.0 /23 + Building4 Data VLAN 356 10 32 14 0 23 255.255.254.0 10.32.14.0 /23 + Building4 Data VLAN 365 10 33 32 0 24 255.255.255.0 10.33.32.0 /24 + Building4 Data VLAN 606 10 0 249 0 29 255.255.255.248 10.0.249.0 /29 + Building4 Data VLAN

The output is:

Building name Building VLAN number VLAN Tag VLAN 1st Octet 1st Octet VLAN 2nd Octet 2nd Octet VLAN 3rd Octet 3rd Octet VLAN 4th Octet 4th Octet VLAN Subnet Slash Mask VLAN Subnet Dotted Mask VLAN Network Subnet VLAN Description Description Building name Building1 VLAN number 330 VLAN 1st Octet 10 VLAN 2nd Octet 32 VLAN 3rd Octet 224 VLAN 4th Octet 0 VLAN Subnet Slash 24 VLAN Subnet Dotted 255.255.255.0 VLAN Network 10.32.224.0 /24 VLAN Description Data VLAN Building name Building2 VLAN number 340 VLAN 1st Octet 10 VLAN 2nd Octet 32 VLAN 3rd Octet 244 VLAN 4th Octet 0 VLAN Subnet Slash 24 VLAN Subnet Dotted 255.255.255.0 VLAN Network 10.32.244.0 /24 VLAN Description Data VLAN Building name Building3 VLAN number 350 VLAN 1st Octet 10 VLAN 2nd Octet 32 VLAN 3rd Octet 2 VLAN 4th Octet 0 VLAN Subnet Slash 23 VLAN Subnet Dotted 255.255.254.0 VLAN Network 10.32.2.0 /23 VLAN Description Data VLAN Building name Building4 VLAN number 606 VLAN 1st Octet 10 VLAN 2nd Octet 0 VLAN 3rd Octet 249 VLAN 4th Octet 0 VLAN Subnet Slash 29 VLAN Subnet Dotted 255.255.255.248 VLAN Network 10.0.249.0 /29 VLAN Description Data VLAN

Replies are listed 'Best First'.
Re^3: Printing of Array Hash is Missing Elements
by ELISHEVA (Prior) on Sep 28, 2009 at 20:45 UTC

    Because you are keying your hash by building name, you only get 4 hash entries, one for each unique building name. Each time you add a new line with the same building name, it overwrites the last line you inserted for that building entry. Hence you read in 40 lines but only get out 4.

    If you want to keep each row separate, I would recommend storing your parsed records in an array of hashes and using a customized sort function.

    Instead of $buildings{bldg_name}{first_octet} do something like this:

    my @aBuildings; while (my $line=<MYFILE>) { chomp $line; #... parse line # add a new element to your array of hashes (AoH) my $hLine = { name => $bldg_name number => $vlan_number ... }; push @aBuildings, $hLine; }

    Then in your print out you would use a custom sort to sort the array elements by building name:

    foreach my $hLine (sort { $a->{name} cmp $b->{name} } @aBuildings) { #print out contents of $hLine }

    See sort and perldsc for more information about custom sorting routines and AoH (array of hash) data structures.

    Best, beth

Re^3: Printing of Array Hash is Missing Elements
by ig (Vicar) on Sep 29, 2009 at 09:29 UTC

    Your code splits on commas but your sample data has no commas.

    To produce all the networks in the format you have shown, sorted by building and VLAN, I might do something like the following:

    use strict; use warnings; use Data::Dumper; do { print <<EOF; Building name $_->[8] VLAN number $_->[0] VLAN 1st Octet $_->[1] VLAN 2nd Octet $_->[2] VLAN 3rd Octet $_->[3] VLAN 4th Octet $_->[4] VLAN Subnet Slash $_->[5] VLAN Subnet Dotted $_->[6] VLAN Network $_->[7] VLAN Description $_->[9] EOF } for sort { $a->[8] cmp $b->[8] or $a->[0] <=> $b->[0] } map { [ unpack('(A3 x1)6 (A15 x1)2 A11 x1 A*') ] } <DATA>; __DATA__ 321 10 32 32 0 19 255.255.224.0 10.32.32.0 /19 Building1 Da +ta VLAN 322 10 32 0 24 255.255.255.0 10.32..0 /24 Building1 Da +ta VLAN 323 10 32 128 0 19 255.255.224.0 10.32.128.0 /19 Building1 Da +ta VLAN 324 10 32 96 0 19 255.255.224.0 10.32.96.0 /19 Building1 Da +ta VLAN 325 10 32 160 0 19 255.255.224.0 10.32.160.0 /19 Building1 Da +ta VLAN 326 10 32 192 0 19 255.255.224.0 10.32.192.0 /19 Building1 Da +ta VLAN 327 10 32 64 0 19 255.255.224.0 10.32.64.0 /19 Building1 Da +ta VLAN 328 10 32 248 0 22 255.255.252.0 10.32.248.0 /22 Building1 Da +ta VLAN 329 10 32 0 24 255.255.255.0 10.32..0 /24 Building1 Da +ta VLAN 330 10 32 224 0 24 255.255.255.0 10.32.224.0 /24 Building1 Da +ta VLAN 331 10 32 225 0 24 255.255.255.0 10.32.225.0 /24 Building2 Da +ta VLAN 332 10 32 228 0 24 255.255.255.0 10.32.228.0 /24 Building2 Da +ta VLAN 333 10 32 229 0 24 255.255.255.0 10.32.229.0 /24 Building2 Da +ta VLAN 334 10 32 232 0 24 255.255.255.0 10.32.232.0 /24 Building2 Da +ta VLAN 335 10 32 233 0 24 255.255.255.0 10.32.233.0 /24 Building2 Da +ta VLAN 336 10 32 236 0 24 255.255.255.0 10.32.236.0 /24 Building2 Da +ta VLAN 337 10 32 237 0 24 255.255.255.0 10.32.237.0 /24 Building2 Da +ta VLAN 338 10 32 240 0 24 255.255.255.0 10.32.240.0 /24 Building2 Da +ta VLAN 339 10 32 241 0 24 255.255.255.0 10.32.241.0 /24 Building2 Da +ta VLAN 340 10 32 244 0 24 255.255.255.0 10.32.244.0 /24 Building2 Da +ta VLAN 341 10 32 245 0 24 255.255.255.0 10.32.245.0 /24 Building3 Da +ta VLAN 342 10 32 0 24 255.255.255.0 10.32..0 /24 Building3 Da +ta VLAN 343 10 32 0 24 255.255.255.0 10.32..0 /24 Building3 Da +ta VLAN 344 10 32 0 24 255.255.255.0 10.32..0 /24 Building3 Da +ta VLAN 345 10 32 0 24 255.255.255.0 10.32..0 /24 Building3 Da +ta VLAN 346 10 32 0 24 255.255.255.0 10.32..0 /24 Building3 Da +ta VLAN 347 10 32 0 24 255.255.255.0 10.32..0 /24 Building3 Da +ta VLAN 348 10 32 0 24 255.255.255.0 10.32..0 /24 Building3 Da +ta VLAN 349 10 32 0 24 255.255.255.0 10.32..0 /24 Building3 Da +ta VLAN 350 10 32 2 0 23 255.255.254.0 10.32.2.0 /23 Building3 Da +ta VLAN 351 10 32 4 0 23 255.255.254.0 10.32.4.0 /23 Building4 Da +ta VLAN 352 10 32 6 0 23 255.255.254.0 10.32.6.0 /23 Building4 Da +ta VLAN 353 10 32 8 0 23 255.255.254.0 10.32.8.0 /23 Building4 Da +ta VLAN 354 10 32 10 0 23 255.255.254.0 10.32.10.0 /23 Building4 Da +ta VLAN 355 10 32 12 0 23 255.255.254.0 10.32.12.0 /23 Building4 Da +ta VLAN 356 10 32 14 0 23 255.255.254.0 10.32.14.0 /23 Building4 Da +ta VLAN 365 10 33 32 0 24 255.255.255.0 10.33.32.0 /24 Building4 Da +ta VLAN 606 10 0 249 0 29 255.255.255.248 10.0.249.0 /29 Building4 Da +ta VLAN

    But the sorting appears to be unnecessary as the data you have provided is already sorted.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://797996]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found