Perl script help on adding variables in a loop

Hello Experts,

I need your help. I'm trying to write a Perl script which will get me the disk free space information in a solaris machine. But I'm stuck. Basically i would like to know the way for adding a particular variable cumulatively in a foreach loop. Below given is the portion of my code (full program attached in as file), where in a foreach loop, I'm matching some pattern from prtvtoc output of each disk and trying to extract a value in a variable $sect_cnt and then i want to do a cumulative addition of this variable, by which i can find the number of free sector available in the disk. Could you please help me in this?

foreach(@prtres)
        {
                if($_ =~ m/^[^\*]/)
                {
                 ($somethg, $slce, $tag, $flags, $first_sect, $sect_cnt, $Lst_sect, $mntdir)=split /\s+/, $_;
                        print "$dsknum[$i]:$_", "\n";
                        print $sect_cnt += $sect_cnt, "\n";
                }
        }
 $i++;
}


Thanks,
Ashok
diskfree.txt
rdashokrajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam314Commented:
I'm not familar with the prtvtoc program.  Is any of the information it provides related to used and/or free space?

If you want to have a cumulative $sect_cnt at the end of the foreach loop
	my $sect_cnt_cum;
	foreach(@prtres) {
		if($_ =~ m/^[^\*]/) {
			($somethg, $slce, $tag, $flags, $first_sect, $sect_cnt, $Lst_sect, $mntdir)=split /\s+/, $_;
			print "$dsknum[$i]:$_", "\n";
			print $sect_cnt += $sect_cnt, "\n";
			$sect_cnt_cum += $sect_cnt;
		}
	}
	print "Cumulative sect_cnt: $sect_cnt_cum\n";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rdashokrajAuthor Commented:
Thanks Adam, I will try to implement your logic and let you know.
0
Adam314Commented:
I'm not sure if the info by prtvtoc is the same, but you might want to look at these modules:

Filesys::Df:
    http://search.cpan.org/~iguthrie/Filesys-Df-0.92/Df.pm

Filesys::DiskSpace:
    http://search.cpan.org/~ftassin/Filesys-DiskSpace-0.05/lib/Filesys/DiskSpace.pm
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

rdashokrajAuthor Commented:
Adam, I could use your addition logic and make it working for me. I need one more help.
Is it possible to extract the value of $sect_cnt variable, when $slce is 2 (you can see this in second column under Slice heading) and then subtract this value from $sect_cnt_cum.

Basically, I'm trying to get the value of "Total number of Sectors availabe" - "Total number of sectors used"

0
rdashokrajAuthor Commented:
Sorry forgot to attached the modified code
dskfree.txt
0
Adam314Commented:

if($slce == 2) {
    $sect_cnt_cum -= $sect_cnt;
}
else {
    $sect_cnt_cum += $sect_cnt;
}
 
 
Or, in shorter form:
$sect_cnt_cum += (($slce==2) ? -1 : 1) * $sect_cnt;

Open in new window

0
rdashokrajAuthor Commented:
Adam, Thanks a lot for your quick response!!!!  I got the result i want. This is the first time in my life, i have written a script (ofcourse with your help :-) which i consider really worth it. Thanks again!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.