Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Are there any tricks with split? 2

Status
Not open for further replies.

ejaggers

Programmer
Feb 26, 2005
148
0
0
US
The way I pick a field using split is (for instance):
$list = 'a b c d e f g';
(@x) = split(' ',$list);
$myvalue = $x[5];

Is there a way to tell split which field you want?

($myvalue) = split(' ',???) only return the 6th field???
 
Note that this is not unique to split(), you can use slices with any list or functions that return lists.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm new to perl and was messing around with split. I came up with this little script just to mess around and it works but is this the best way to do what I'm doing here or are there more efficient ways to do this? I guess this might not be directly related to split because that part seems to work fine so sorry if OT. Thanks

Code:
#!/bin/perl
use strict;
use warnings;

use Win32::OLE('in');

use constant wbemFlagReturnImmediately => 0x10;
use constant wbemFlagForwardOnly => 0x20;

my @computers = ("DS-ITSHOP1");
foreach my $computer (@computers) {
	my $objWMIService = Win32::OLE->GetObject("winmgmts:\\\\$computer\\root\\CIMV2") or die "WMI connection failed.\n";
	my $colItems = $objWMIService->ExecQuery("SELECT * FROM Win32_Environment", "WQL",
				wbemFlagReturnImmediately | wbemFlagForwardOnly);
	foreach my $objItem (in $colItems) {
			if ($objItem->{Name} eq "Path"){
				print "Name: $objItem->{Name}\n";
				my $data = "$objItem->{VariableValue}\n";
				my @values = split(';', $data);
				foreach my $val (@values) {
					print "$val\n";
				}
			}
	}
}
 
unless there is already a method you can use that returns the results as an array/list then your code seems OK to me.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top