vlturner has asked for the wisdom of the Perl Monks concerning the following question:

Ugh, my tea hasn't kicked in and I need a nudge please. I have 250K row of 1K data. Element 77 is the application name and element 93 is the IP Address. I need to create an array off applications, and the member IP address that belong to it. For some reason I am placing the string name of the application where a numeric is expected. This is just the snippet of code not reflective of its true placement. Alas, beer consumtion over Sunday Night Football, has lowered my Monday morning cafein levels. Need loaner brain cells....

my @members; my $key = \@members; my @application; print "Application = ", $data[$application_pos], "\n"; if ( $data[$application_pos] ne "" && $data[$ip_address_pos] n +e "" ) { $key = $data[$application_pos]; $application[$key] = $data[$application_pos]; push $application[$key], $data[$ip_address_pos]; print Dumper( @application ) ; }

Replies are listed 'Best First'.
Re: Array of Arrays on Monday.
by stevieb (Canon) on Jan 25, 2016 at 15:25 UTC

    You need to show us the code where you're getting $application_pos and $ip_address_pos.

    Do you have use strict; and use warnings; enabled? This line: push $application[$key], $data[$ip_address_pos]; should have thrown some red flags. It helps if you supply us with a snippet of working code that demonstrates your issue.

    Update: Actually, your whole code snip looks suspicious. Please just let us know what you want the end result to look like. I'm getting a feeling you're wanting a hash here.

      the array should look as follows. For each of 250K lines I should push the application name, and then the IP address to the proper application name. Only keeping the unique changes. One application may end up with thousands of members. and there are over 4000 unique application names. I need to create a CSV file of application names by member and the member column will use the ";" as a separator not to interfere with the comma.

      A = ( application1 => [ "10.10.10.1", "10.10.10.2" ], application2 => [ "10.10.10.3", "10.10.10.4", "10.10.10.5" ], application3 => [ "10.10.10.1", "10.10.10.3", "10.10.10.6" ], );
        So, don't you miss a % before the A, i.e. aren't you trying to create a hash of arrays rather than an array of arrays?
        my %application; # <-- Hash! print "Application = ", $data[$application_pos], "\n"; if ( $data[$application_pos] ne "" && $data[$ip_address_pos] ne "" ) { my $key = $data[$application_pos]; push @{ $application{$key} }, $data[$ip_address_pos]; # Pushing t +o a HoA. print Dumper( \%application ) ; }

        Update: Fixed signature.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        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

      That section of code works fine, they return the numeric 77 for $application_pos the column and 62 for the $ip_address_pos. Yes, both strict and warnings are defined and adheared to...

        Could you possibly show us the code that isn't working? Posting code that you says works doesn't help us :)

Re: Array of Arrays on Monday.
by AnomalousMonk (Archbishop) on Jan 25, 2016 at 20:08 UTC