in reply to Re: Data manipulation on a file
in thread Data manipulation on a file

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 ?

Replies are listed 'Best First'.
Re^3: Data manipulation on a file
by toolic (Bishop) on Sep 30, 2015 at 15:16 UTC
Re^3: Data manipulation on a file
by SuicideJunkie (Vicar) on Sep 30, 2015 at 17:13 UTC

    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}}," }

        Instead of printing it, append it to a variable:

        my $output = ''; for ( ... ) { $output .= "$file:$@$filer_hash{$filer}},"; } print "Now have: $output\n";

        Small problem now that the output when run on a large amount of linux hosts is producing duplicate mount points for different shares etc. This is due to the many mounts on the same mountpoint.

        like this $ouputstring: host1: mountpoint1 mountpoint1 mountpoint1 mountpoint2 host25: mountpoint5 mountpoint5 mounpoint5 mountpoint6 m......78.

        it would be really nice to remove the duplicates but this would mean logic before updating the array elements or logic coming out of the dump. As I am just dumping the full hash into a $scalar this could get tricky. Anyone got any suggestions on removing the duplicate mount points once the hash has been dumped to the scalar or should I put logic in the dump or even into the push. Perl is so powerfully but still a newbie here so any help appreciated. Many thanks
        #!/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 ) { $outputstring .= "$filer:@{$filer_hash{$filer}}," } print " This is my output : $outputstring