in reply to Need to get hash of arrays

What Discipulus said:

Win8 Strawberry 5.8.9.5 (32) Fri 05/14/2021 4:48:55 C:\@Work\Perl\monks\workInProgress12 >perl use strict; use warnings; use autodie; use Data::Dump qw(dd); my $data = <<'EOD'; ColA | ColB | 500001| Network1| 500003| Network1| 500002| Network2| 500001| Network2| 500003| Network3| EOD open my $fh, '<', \$data; <$fh>; # ignore headings record/line my %hash4; while (<$fh>) { my ($k, $v) = map { (my $r = $_) =~ s{ \A \s+ | \s+ \z }{}xmsg; $r; } split /\s*\|\s*/ #splits $_ implicitly ; # print "\$k '$k' \$v '$v' \n"; # for debug push @{ $hash4{$k} }, $v; } dd \%hash4; close $fh; ^Z { 500001 => ["Network1", "Network2"], 500002 => ["Network2"], 500003 => ["Network1", "Network3"], }
You really should know all this by now. (sigh)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Need to get hash of arrays
by haukex (Archbishop) on May 14, 2021 at 09:13 UTC

    Or with Text::CSV/Text::CSV_XS:

    use warnings; use strict; use Data::Dump; use Text::CSV; # also install Text::CSV_XS for speed my %data; my $csv = Text::CSV->new({ binary=>1, auto_diag=>2, sep_char=>"|", allow_whitespace=>1 }); my $hdr = $csv->getline(*DATA); while ( my $row = $csv->getline(*DATA) ) { push @{ $data{ $row->[0] } }, $row->[1]; } $csv->eof or $csv->error_diag; dd \%data; __DATA__ ColA | ColB | 500001| Network1| 500001| Network2| 500002| Network2| 500003| Network1| 500003| Network3|

      Hello Smart Monks, Thanks for your help. I have fixed this by below assignment

      push @{ $hash4{$cellp[$_]} }, $cellr[$_] for (0..$#cellp);

      I have fixed by below and lable as appropriate about within network or no network users

      if (grep { grep { $_ eq "No Network" } @{$hash4{$celln[$o]}} } keys %h +ash4) { worksheet->write($r12, 2, "Non Network"); }

      Thanks for your help.

Re^2: Need to get hash of arrays
by LanX (Saint) on May 14, 2021 at 09:06 UTC
    > You really should know all this by now. (sigh)

    why learning if he gets it for free? (sigh ;)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      True, true...


      Give a man a fish:  <%-{-{-{-<