Here's the code I came up with for testing whether the line read contains a
: (and thus makes it eligible for SPLITting).
#!/usr/bin/perl -w
$filename = "/home/ron/perl/cpu.txt";
$output = system("cat /proc/cpuinfo>$filename");
open(INFILE,"< $filename");
while (<INFILE>) {
$linetest =$_;
if ($linetest=~ m/:/){ #does the line have a : in it?
my ($key, $value) = split(/:/,$_); #if it does, split it at the :
print ("$key \=\> $value\n"); #print out the new values
}
# insert hash assignment code
}
>From here, if I knew how to do it, I'd assign $key and $value to a hash for
each line read in...
I messed with it a little but wasn't able to get it to work... I tried this
(unsucessfully):
%myhash{$value=>$key};
I'm sure there is a better way to do this....
Ron
-----Original Message-----
From: Anders Holm [mailto:[EMAIL PROTECTED]]
Sent: Monday, April 29, 2002 9:16 AM
To: Michael Lamertz
Cc: beginners perl
Subject: RE: Getting contens of file into a hash
Thank you Micheal!!
Having loads of fun with this at the moment!! ;)
There seems to be another caveat as well, SMP systems. ;) Fortunate enough
to be able to test it on such a box at work.
So, input would be something of the following:
[anders@redhat anders]$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 8
model name : Pentium III (Coppermine)
stepping : 10
cpu MHz : 997.497
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca
cmov pat pse36 mmx fxsr sse
bogomips : 1992.29
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 8
model name : Pentium III (Coppermine)
stepping : 10
cpu MHz : 997.497
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca
cmov pat pse36 mmx fxsr sse
bogomips : 1992.29
[anders@redhat anders]$
There are now 2 empty lines that'd need to be stripped out, or I'll be
getting :
Use of uninitialized value in hash element at ./cpuinfo.pl line 27,
<CPUINFO> line 19. Use of uninitialized value in hash element at
../cpuinfo.pl line 27, <CPUINFO> line 19. Use of uninitialized value in hash
element at ./cpuinfo.pl line 27, <CPUINFO> line 38. Use of uninitialized
value in concatenation (.) at ./cpuinfo.pl line 33, <CPUINFO> line 38.
Line 19 & 38 are the empty lines. Shouldn't be too hard to fix this either..
I'll bounce my head on it and shout if I can't get it to work!! ;)
Cheers!!
Anders Holm
-----Original Message-----
From: Michael Lamertz [mailto:[EMAIL PROTECTED]]
Sent: 29 April 2002 13:29
To: Michael Lamertz
Cc: beginners perl; Anders Holm
Subject: Re: Getting contens of file into a hash
Whooops:
On Mon, Apr 29, 2002 at 02:12:27PM +0200, I wrote:
> On Mon, Apr 29, 2002 at 11:59:42AM +0100, Anders Holm wrote:
> > Hi folks!
> >
> You're right on track (with some exceptions, but read about that
> further down the page).
>
> my ($key, $value) = split /:/;
make that 'split /\s*:\s*/;'
> will give you e.g.
> $key "cpu family"
> $value "6"
--
If we fail, we will lose the war.
Michael Lamertz | +49 221 445420 / +49 171 6900
310
Nordstr. 49 |
[EMAIL PROTECTED]
50733 Cologne |
http://www.lamertz.net
Germany |
http://www.perl-ronin.de
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]