View Single Post
Old 11-28-2011, 02:26 PM   PM User | #22
joeDetroit
New Coder

 
Join Date: Nov 2011
Location: Detroit
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
joeDetroit is an unknown quantity at this point
I tried putting
Code:
$licenses{ $ENV{'COMPUTERNAME'} } = shift @{ $licenses{ 'UNASSIGNED' } };
in my existing while... loop, and I get the following error:

Odd number of elements in anonymous hash at k:\Packages\Packages\MirrorFolder4.1\test5.pl line 42, <STDIN> line 1.

Line 42 is
Code:
if ( {keys %licenses} =~ m/$strSearchFor/i){
which (theoretically) looks for the word 'unassigned' in the hash...

Here's my full code:
Code:
#!/usr/bin/perl

use warnings;
use strict;
use File::Copy;
use Data::Dumper;

my $path   = 'k:/Packages/Packages/MirrorFolder4.1';
my $file   = "$path/testlicense.txt";
my $backup = "$path/testlicense.bak";
my $newFile = "$path/new-license.txt";

my $strSearchFor = "unassigned";
my $setCount = 0;
my %licenses;


#make a backup of the license file just in case
#copy($file, $backup) or die "failed to copy '$file' to '$backup' $!";

open my $license_fh, '<', $file or die "failed to open '$file' $!";

LICENSE:
while ( my $line = <$license_fh> ) {
    chomp $line;
    next LICENSE if $line =~ /^\s*$/;
    
    # this will need to be adjusted depending on your actual field separator
    my ($hostname, $license) = split / /, $line, 2;
    unless ($hostname and $license) {
        print Dumper $line;
        next LICENSE;
    }
    push @{ $licenses{$hostname} }, $license;
}

print Dumper \%licenses;

while($setCount == 0){
	print "while...\n" . "setcount- " .$setCount;
	$_ = <STDIN>;
		if ( {keys %licenses} =~ m/$strSearchFor/i){ 
			print "found it...";
			$_ = <STDIN>;
			$licenses{ $ENV{'COMPUTERNAME'} } = shift @{ $licenses{ 'UNASSIGNED' } };
			#$line =~ s/$strSearchFor/$ENV{'ComputerName'}/i;
			print "substitued...";
			$_ = <STDIN>;
			$setCount = 1;
			print "setcount- " . $setCount;
			$_ = <STDIN>;
			open (NEWLICENSE, ">$newFile") or die "sorry. $!";
			#print ($setCount);
			print NEWLICENSE (%licenses);
			close (NEWLICENSE);
		}
}
Thanks for your help!
Joe
joeDetroit is offline   Reply With Quote