Hi folks! 1st of all: Thanks to Ron & Michael for giving me ideas and helping me out!!
Here is the code resulting from my excercise. May be useful for others, who knows!! I'll be using it anyway, since I'm planning on kicking off my own Linux Distro. Will be using it for figuring out optimization parameters for building packages.. ;) Rip it apart or do whatever else you may feel that you just have to do with it.. Here we go then!! #!/usr/bin/perl -W # # This script created for educational purposes. # Intention is to check for number of cpu's and their type. # $author = "Anders Holm"; $version = "0.1"; # Opening the file /proc/cpuinfo for input... open (CPUINFO, "</proc/cpuinfo") || die "Can't open file \/proc\/cpuinfo!! : $!\n"; # Printing some program info... print "\nCreated by $author. Version $version\n"; print "With the immortal help from the people on beginners\@perl.org!!\n\n"; while (<CPUINFO>) { chomp; #removing newline $linetest =$_; #checking if the line contains a : if ($linetest=~ m/:/) { ($key, $value) = split /\s*:\s*/; #if so, split the line in 2 for ($key) { $key =~ s/ /_/; #replace spaces with _ } # print "$key : $value\n"; #and print the hash $cpuinfo{$key} = $value; #and put the keys & and values in a hash } } # close the filehandle since it is no longer needed. close (CPUINFO); # print the number of CPU's in this system. $num_cpus = $cpuinfo{"processor"} +1; print "\nNumber of CPU's = $num_cpus\n"; #print the type (i3|4|5|686) of processor $cpu_type = $cpuinfo{'cpu_family'}; print "CPU type is i" . "$cpu_type" . "86\n\n"; Best Regards Anders Holm -----Original Message----- From: Ron Powell [mailto:[EMAIL PROTECTED]] Sent: 29 April 2002 15:11 To: 'Anders Holm'; Michael Lamertz Cc: beginners perl Subject: RE: Getting contens of file into a hash 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] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]