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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convering a command output into hash

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
HI,
my winshares.cmd command returns the following structure:
ows shares for KDI-PGY2500

ADMIN$ D:\WINNT
AraxiHome D:\Prinergy
AraxiPreps D:\Prinergy\CreoAraxi\AraxiPreps
AraxiTemp_KDI-PGY2500_E E:\AraxiTemp
AraxiVolume_KDI-PGY2500_J J:\
C$ C:\

I need to convert it inro a hash.I know how to do it using a file.But how do I do it without the file?

Starting by:
my $shares=`winshares.cmd`;

Thanks

Long live king Moshiach !
 
Code:
use strict;
use warnings;

my @shares = qx{winshares.cmd};
chomp @shares;

my %winshares;

foreach (@shares) {
   my ($shname, $shpath) = split(/\s+/, $_);
   $winshares{$shname} = $shpath;
}
Warning: if your paths have spaces in, this won't work...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks,great

If some paths have spaces,what would be a solution?
Like:

LinkTemp D:\Program Files\SynapseLink\2.1\yoho\bin\Temp


Long live king Moshiach !
 
Actualy,the above code does not work - it prints nothing...

for (sort keys %winshares) {
print "SHARE=$_=\n";
}

prints:
SHARE==

Long live king Moshiach !
 
No, because you can have any number of contiguous spaces in a path name. Something like
Code:
foreach (@shares) {
   my @temp = split(/\s/, $_);
   my $shname = shift(@temp);
   my $shpath = join(" ", @temp);
   $shpath =~ s/^\s*(.*)$/$1/;
   $winshares{$shname} = $shpath;
}


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Is the winshares.cmd producing any output? Does it need a path? Try printing out the contents of @shares...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top