Using foreach?? maybe..

Discussion in 'Perl Misc' started by SimonH, Jun 3, 2007.

  1. SimonH

    SimonH Guest

    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=impersonate}!\\\\$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";
    } } } }
    ##################################################
     
    SimonH, Jun 3, 2007
    #1
    1. Advertising

  2. SimonH

    Brad Baxter Guest

    On Jun 3, 2:10 pm, Bob Walton <> 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
     
    Brad Baxter, Jun 5, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    745
    Scott Allen
    Feb 12, 2005
  2. caldera
    Replies:
    1
    Views:
    4,406
    Scott Allen
    Feb 22, 2005
  3. Guest
    Replies:
    5
    Views:
    620
  4. Ville Vainio
    Replies:
    11
    Views:
    615
    Hamish Lawson
    Aug 10, 2004
  5. Kevin Blount
    Replies:
    9
    Views:
    2,653
    Kevin Blount
    Aug 29, 2006
Loading...

Share This Page