Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using foreach?? maybe..

Reply
Thread Tools

Using foreach?? maybe..

 
 
SimonH
Guest
Posts: n/a
 
      06-03-2007
Hi!

Im trying to change the following Ive worked on into using a list of
machines in a file called machine.txt
At the moment, Ive used an array of computer names im testing on. This
works, but Id like some direction on how to reference a file instead of
using an array. Any help greatly appreciated. Thank you.
I believe I might have to use a foreach statement and possibly a split
function on the machine.txt file, but not sure where to start with this.

The format of the machine.txt file is:

dell101
dell102
dell103
etc

#use diagnostics;
#use strict;
use Win32::OLE('in');

# Use the Win32_Process
my @requests = ('Win32_Process', );

# Computer list
my @computers = qw/dell101 dell102 dell103/; # having trouble
here....instead of listing, id like to reference a file called machine.txt

# Connect to each systems CimV2 repository in turn.
for my $computer( @computers ){
my $wmi_repos =
Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersona te}!\\\\$computer\\root\\CIMV2")
or die "WMI connection failed: ".Win32::OLE->LastError()."\n";

for my $request( @requests ){
my $wmi_collection = $wmi_repos->ExecQuery("SELECT * FROM $request", "WQL",
48 )
or die "WQL query failed: ".Win32::OLE->LastError()."\n";

for my $item (in $wmi_collection){
my @methods = join (", ", map {$_->{Name}}( in $item->{Methods_} ) );
my %properties = map { $_->{Name} => $_->{Value} }( in
$item->{Properties_} );

# Print results
print "\n$computer :: $request :: $properties{Name}\n", "\tMethods:
@methods\n\tProperties:\n";
for my $property( sort keys %properties ){
( $properties{$property} )
? print "\t\t$property = $properties{$property}\n"
: print "\t\t$property =\n";
} } } }
##################################################


 
Reply With Quote
 
 
 
 
Brad Baxter
Guest
Posts: n/a
 
      06-05-2007
On Jun 3, 2:10 pm, Bob Walton <(E-Mail Removed)> wrote:
> SimonH wrote:
> > The format of the machine.txt file is:

>
> > dell101
> > dell102
> > dell103
> > etc

>
> Try something like:
>
> use warnings;
> use strict;
> my $fh;
> my @computers;
> open $fh,"<","machine.txt"
> or die "Oops, couldn't open machine.txt for input, $!";
> while(<$fh>){
> chomp;
> push @computers,$_;
> }
> close($fh);
> #...
>
> ...> # Computer list
> > my @computers = qw/dell101 dell102 dell103/; # having trouble
> > here....instead of listing, id like to reference a file called machine.txt


Or a little more succinctly,

open my $fh,"<","machine.txt"
or die "Oops, couldn't open machine.txt for input, $!";
my @computers = <$fh>;
chomp @computers;
close($fh);

However, that slurps the whole file, which it looks like
you don't need. Instead, you could:

open my $fh,"<","machine.txt"
or die "Oops, couldn't open machine.txt for input, $!";

while( defined( my $computer = <$fh> ) ) {
chomp $computer;

# do all your other stuff with $computer

}
close($fh);

# or more succinctly ...

open $fh,"<","machine.txt"
or die "Oops, couldn't open machine.txt for input, $!";

while( <$fh> ) {
chomp;

# do all your other stuff with $_

}
close($fh);


And please improve your indentation.

--
Brad

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a Link Button to redirect to another page by using data from =?Utf-8?B?R1REcml2ZXI=?= ASP .Net 1 02-16-2005 07:04 PM
Error page using the Application_Error void dosnt work when using DIV for a please wait message s_erez@hotmail.com ASP .Net 2 12-24-2004 12:11 PM
no code in webform using vs.net, but in webform using notepad timmso ASP .Net 1 12-12-2003 04:30 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments