Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Processing array with suitable options

by Bharath666 (Novice)
on Feb 22, 2013 at 10:27 UTC ( #1020125=perlquestion: print w/ replies, xml ) Need Help??
Bharath666 has asked for the wisdom of the Perl Monks concerning the following question:

My requirement as follows: I am reading the output of the wmic command as follows:

my @pnames = `wmic product where "Name like '%Provider%'" get Iden +tifyingNumber, Name,Version`; print "@pnames";
**Till here the output is as follows:**
IdentifyingNumber Name + Version {E094B8EA-87B7-48DE-A0A8-A18AC8BFCDF4} .NET Data Provider for Tera +data 14.00.0.0 {1551F9D6-1B14-4AE1-BABA-70A4319C236A} ODBC Driver Provider + 14.00.0.0

**Proceesing the array**

foreach $name ( @pnames ) { chomp($name); #@names = split('need to pass something',$name); # I want to split every input entry into 3 parts- IdentifyingN +umber, Name,Version similarly the way they read. $name =~ s/^\s+//; #remove leading spaces $name =~ s/\s+$//; #remove trailing spaces # here logic goes this way, If the version number has matched, s +erach for name and if name has matched display its identifying number + print "Identifying number(say $id) of the packge with name(say $ +pkg)\n"; }

I am in urgent requirement. Any other approach is also greatly appreciated. Can any one please help me?? Thanks in advance

Comment on Processing array with suitable options
Select or Download Code
Re: Processing array with suitable options
by Anonymous Monk on Feb 22, 2013 at 10:36 UTC
    read split, esp the part about special characters, then use one (like ' ' )
Re: Processing array with suitable options
by tobyink (Abbot) on Feb 22, 2013 at 10:44 UTC

    Don't use split; use a regular expression with capture groups.

    #!/usr/bin/perl use 5.010; use strict; use warnings; my @pnames = <DATA>; for (@pnames) { chomp; / ^\s* # allow leading spaces \{(.+?)\} # IdentifyingNumber in braces \s+ # white space (.+?) # Name \s+ # white space ([0-9\.]+) # Version (allow numeric plus dot) \s*$ # allow trailing space /x or next; # skip lines not conforming to above pattern say "Got Identifying Number: $1"; say "Got Name: $2"; say "Got Version: $3"; say "----"; } __DATA__ IdentifyingNumber Name + Version {E094B8EA-87B7-48DE-A0A8-A18AC8BFCDF4} .NET Data Provider for Terada +ta 14.00.0.0 {1551F9D6-1B14-4AE1-BABA-70A4319C236A} ODBC Driver Provider + 14.00.0.0
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Processing array with suitable options
by vinoth.ree (Parson) on Feb 22, 2013 at 10:53 UTC

    May be worst method,

    use strict; use warnings; use Data::Dumper; #foreach $name ( @pnames ) while(<DATA>) { next if (/^\s*$/); chomp($_); $_ =~s/ {1,}/ /; my @names = split(/ {3,}/, $_); print Dumper \@names; } __DATA__ IdentifyingNumber Name + Version {E094B8EA-87B7-48DE-A0A8-A18AC8BFCDF4} .NET Data Provider for Teradata + 14.00.0.0 {1551F9D6-1B14-4AE1-BABA-70A4319C236A} ODBC Driver Provider + 14.00.0.0
Re: Processing array with suitable options
by RichardK (Priest) on Feb 22, 2013 at 11:48 UTC

    The supplied data looks like it's in fixed width columns, so maybe unpack would do a better job?

Re: Processing array with suitable options
by soonix (Curate) on Feb 22, 2013 at 12:21 UTC
    In this case you can make your life easier by changing the command: wmic can output comma-separated:
    wmic product where "Name like '%Provider%'" get IdentifyingNumber, Nam +e, Version /format:csv
    then you can split(/,/$name) ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020125]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2014-09-21 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls