in reply to Data manipulation on a file

This creates a hash-of-arrays:
use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys=1; my %hosts; for (qx(mount | grep 'type nfs' | awk -F/ '{print \$1,\$3}' | sort)) { chomp; my ($host, $mp) = split; push @{ $hosts{$host} }, $mp; } print Dumper(\%hosts);

See also:

Replies are listed 'Best First'.
Re^2: Data manipulation on a file
by sstruthe (Novice) on Sep 30, 2015 at 15:10 UTC

    wow thanks very much for the quick response, is there a way of doing this just as simple without using data dumper. Would like to format the output to my style ?

      Loop over for my $hostname (keys %hosts), or if you like, over for my $hostname (sort keys %hosts) so the host names come out in asciibetical order.

      Between $hostname and join ':', @{ $hosts{$hostname} }, you've then got everything you need to concatenate onto your output string inside the loop. Make sure your output variable is defined outside the loop so you're not just throwing the results away when the loop ends.

      %hosts might be more clear if named %mountPoints or %hostToMountPoints if you don't mind the typing.

        Thanks for the replies, I am just about there with what I am needing. The print comes out nicely to STD out however I am a little confused as to how I assign the output to a scalar $output = . In the for loop what syntax is needed to assign the result to a variable concatinating each time. Once the loop of the hash is assigned to a $output then I can take it from there. Many thanks and sorry for the silly quesitons

        #!/usr/bin/perl use strict; use warnings; my $filer; my %filer_hash; for (qx(mount -t nfs | awk -F/ '{print \$1,\$3}' | sed -r 's/(blah.*:) +|(bblah.*:)//g' |sort)) { chomp; my ($host, $mp) = split; push @{ $filer_hash{$host} }, $mp; } foreach $filer ( sort { ${filer_hash{$b}} <=> ${filer_hash{$a}} } keys + %filer_hash ) { print "$filer:@{$filer_hash{$filer}}," }