Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
change a perl prog

Posted by mike21 (mike21), 22 November 2004
hello ,

this is what i have in /proc/meminfo (linux machine) :

    total:    used:    free:  shared: buffers:  cached:
Mem:  261390336 210886656 50503680        0 56795136 84643840
Swap: 536731648        0 536731648
MemTotal:       255264 kB
MemFree:         49320 kB
MemShared:           0 kB
Buffers:         55464 kB
Cached:          82660 kB
SwapCached:          0 kB
Active:         131360 kB
ActiveAnon:       5940 kB
ActiveCache:    125420 kB

I write a prog in perl but it doesn't work because i'd like to read those 3 numbers :
261390336 210886656 50503680 of the Mem....
and my prog , here :

#! /usr/bin/perl -w

use strict ;

# Recuperation du contenu du fichier
open LOAD, "</proc/meminfo" or die "Impossible d'acceder a memoire";
chomp $meminfo;
close LOAD;

# affiche les valeur de meminfo
my @charges = split (" ", $meminfo);

# Traitement
print "taille de ma memoire $charges[0]\n" ;
print "taille de la memoire utiliser $charges[1]\n" ;
print "taille de la memoire libre $charges[2]\n" ;
print "\n" ;

give me that:

taille de ma memoire total:
taille de la memoire utiliser used:
taille de la memoire libre free:

i would like to display Mem line in place of total, used, free...

thanx very much


Posted by John_Moylan (jfp), 22 November 2004
Try this:

#! /usr/bin/perl -w

use strict ;

# Recuperation du contenu du fichier
open LOAD, "</proc>;
 my @meminfo = <LOAD>; # Note: This line pulls in an array of lines from the file
close LOAD;

# affiche les valeur de meminfo
my @charges = split (" ", $meminfo[1]); # array element 1 is the one we're interested in so lets split only that line

# Traitement
print "taille de ma memoire $charges[1]\n" ; # element [0] is the string ' Mem:'  the ones we want start at element [1]
print "taille de la memoire utiliser $charges[2]\n" ;
print "taille de la memoire libre $charges[3]\n" ;
print "\n" ;

I have noted any changes I made in the code

Posted by John_Moylan (jfp), 22 November 2004
Just to update what was happening in your code:

This pulls in the first line of output only, this means the only data that the scalar $meminfo *ever* held was:
total:    used:    free:  shared: buffers:  cached:

If you really wanted *all* the output you would have to undefine a special Perl variable by adding "undef($/);" to your code before your read your file.

What does $/ do?
$/ is the input record separator, this is set by default to 'newline, and thats exactly what it did for you, only gave you everything up to the newline.
By undefining the variable it will have nothing to separate on and therefore gives you everything.

: Start contradict mode
 Personally I avoid slurping a complete file cos it can take up a fair chunck of memory, I also use it a lot!  
: End contradict mode

In this example I think getting an array of lines (as my code shows) is more elegant than slurping. I'm sure others will correct me here if I'm wrong.


Posted by mike21 (mike21), 23 November 2004

i just would like to know how can i write this code in PERL :

if charge > seuil2
     Afficher (critical)
     if charge > seuil1
        Afficher (warning)

thanx in advance

Posted by admin (Graham Ellis), 23 November 2004
Something like

if ($charge > $seuil2) {
       Afficher("critical") ;
} else {
       if ($charge > $seuil1) {
               Afficher ("warning") ;

but it depends on what was a variable, what was a constant and what was a subroutine name in your original code.

Posted by mike21 (mike21), 24 November 2004
thanx ,

in fact my question is
I would like to know how can i read the names of the process which running and and how can i display it on an array?

then i would like to do a comparaison , if the process X doesn't run , then print "ALERT !!"
I show you teh beginnig of my prog but...

#! /usr/bin/perl -w

use strict ;

open(PS_F, "ps -A | grep man |");
while (<PS_F>) {
($PID,$TTY,$TIME,$CMD) = split;

cheers all

Posted by admin (Graham Ellis), 24 November 2004
Je pense que votre francais est plus belle que votre anglais?  C'est un peu difficille point moi a comprerendre votre question   And as you will see from those few words, my French is very poor.

if you add
     push @processes,$CMD;
at the end of your loop, then you'll have a list of all the processes running.   If you add
     $gotit = i if ($CMD =~ /processname/);
at the end of your loop, you'll then know if the process that you are interested in is being run once your loop has exited by checking the $gotit variable.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho