in reply to Re^2: Array of Arrays on Monday.
in thread Array of Arrays on Monday.

Only keeping the unique changes

If this means ignoring duplicates use a HashOfHashes (HOH)

#!perl use strict; use Data::Dumper; my %hash=(); while (<DATA>){ chomp; my ($name,$ip) = (split)[0,1]; if ( $name ne "" && $ip ne "" ) { ++$hash{$name}{$ip}; } } my %application=(); for my $name (keys %hash){ $application{$name} = [sort keys %{$hash{$name}}]; } print Dumper( \%application ) ; __DATA__ application1 10.10.1 application1 10.10.2 application3 10.10.1 application3 10.10.3 application2 10.10.3 application2 10.10.4 application2 10.10.5 application3 10.10.6 application1 10.10.1 application1 10.10.1
poj

Replies are listed 'Best First'.
Re^4: Array of Arrays on Monday.
by vlturner (Sexton) on Jan 25, 2016 at 19:58 UTC

    I need Hash of Hashes.... But here,s the rub. All these lines are CI records from a ServiceNow CMDB. So I screwed up thinking the application name was on the same line. The application name is only the CI ( CI10335478 ). Now I need to look up the name of the CI and all applications have a sys_class_name = "cmdb_ci_appl" Its just that I need a placeholder for Application Name because the line containing that may be 10000 lines or more down the data. But I need a place holder for application name until it comes along and we can populate it. This is because the ouput data need to look like this.

    Status,Name,Description,Members,Id,InboundUtilizationBandwidth,Outboun +dUtilizationBandwidth,Appliances,CollectOnDirector ,app_name1,CI10335478,MemberIP1;MemberIP2;MemberIP6,,,,, ,app_name2,CI10334984,MemberIP4;MemberIP2,,,,,

    So what I need is to buld out the following array.

    $VAR1 = { 'CI10335478' => 'app_name1' => MemberIP 1, MemberIP 2, MemberIP 6 ], 'CI10334984' => 'app_name2' => MemberIP 4, MemberIP 2, }; our %application = (); sub add_to_application_members { my ($app_ci, $member_ip, $value) = @_; $application{$app_ci{$member_ip}} = $value; } sub add_to_application_names { my ($member, $value) = @_; $application{$member} = $value; } if ( $data[$class_pos] eq "cmdb_ci_appl" ) { add_to_application_names( $data[$ci_pos] => $data[$name_pos] ); print Dumper( %application ) ; } if ( $data[$application_pos] ne "" && $data[$ip_address_pos] ne "" ) { add_to_application_members( $data[$application_pos] => $data[$ip_add +ress_pos] ); print Dumper( %application ) ; } _DATA_ u_ci_id,sys_class_name,application,ip_address ,name CI12345,cmdb_ci_appl , , ,foobar1 CI56789,cmdb_ci_svr ,CI12345 ,10.10.10.10, hostname1

      Would this work ?. You may find it easier to build.

      my %application = ( 'CI10335478' => { name => 'app_name1', IP => [ 'MemberIP 1', 'MemberIP 2', 'MemberIP 6', ], }, 'CI10334984' => { name => 'app_name2', IP => [ 'MemberIP 4', 'MemberIP 2', ], }, );
      poj
Re^4: Array of Arrays on Monday.
by vlturner (Sexton) on Jan 26, 2016 at 14:03 UTC

    poj is on the right track, and for some reason, I got myself confused and am stuck now. So he sent this.

    my %application = ( 'CI10335478' => { name => 'app_name1', IP => [ 'MemberIP 1', 'MemberIP 2', 'MemberIP 6', ], }, 'CI10334984' => { name => 'app_name2', IP => [ 'MemberIP 4', 'MemberIP 2', ], }, );

    I have 250K records in XML that are parsing correctly. So now I need to get them into the hash. $data$name_pos contains the application name on application CI's. $data$application_pos contains the CI of the application we need to look up $data$name_pos on device CI's So $application_pos and $data$ci_pos should be equal. I have to process > 5K of records to start getting intersections of the data, and I don't want to put that here, and its all in XML. So my idea was to write my data to the hash or array when I come across them. So I don't have a blob to just push into the structure.

      Use a separate hash for the names and combine them with IPs at the end

      #!perl use strict; use Data::Dump 'pp'; my %tmp =(); my %name=(); my $header = <DATA>; while(<DATA>){ chomp; my ($u_ci,$class,$app,$ip,$name) = split ',',$_; if ($class eq 'cmdb_ci_appl'){ $name{$u_ci} = $name; } elsif ($class eq 'cmdb_ci_svr'){ $tmp{$app}{$ip}=1; } else { print "$class\n"; } } my %application = (); for my $app (keys %tmp){ $application{$app}{'IP'} = [sort keys %{$tmp{$app}}]; $application{$app}{'name'} = $name{$app}; } pp \%application ; __DATA__ u_ci_id,sys_class_name,application,ip_address ,name CI56789,cmdb_ci_svr,CI10335478,MemberIP 1,hostname1 CI56789,cmdb_ci_svr,CI10335478,MemberIP 2,hostname1 CI56789,cmdb_ci_svr,CI10335478,MemberIP 6,hostname1 CI56789,cmdb_ci_svr,CI10334984,MemberIP 4,hostname1 CI56789,cmdb_ci_svr,CI10334984,MemberIP 2,hostname1 CI10334984,cmdb_ci_appl,,,app_name2 CI10335478,cmdb_ci_appl,,,app_name1
      poj