View Single Post
Old 12-06-2011, 04:02 PM   PM User | #27
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 put your code in, and then got this when I tried to run it:
Code:
Can't use string ("1111-1111-1111-1111") as an ARRAY ref while "strict refs" in use at k:\Packages\Packages\MirrorFolder4.1\test5.pl line 52, <STDIN> line 4.
That's the one that should be assigned (ie- it's the first unassigned license key in the list).

The existing hostname/license key pairs in the data are dumped to the new license file just fine.

Here's the full code after your addition:
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*$/;
    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 ( exists $licenses{$strSearchFor} ) { 
			print "found it..." ;
			$_ = <STDIN>;
			$licenses{ $ENV{'COMPUTERNAME'} } = shift @{ $licenses{ 'unassigned' } };
			print "substitued...";
			$_ = <STDIN>;
			$setCount = 1;
			print "setcount- " . $setCount;
			$_ = <STDIN>;
			open my $license_fh, '>', $newFile or die "failed to open '$newFile' <$!>";
			foreach my $hostname ( keys %licenses ) {
				foreach my $license ( @{ $licenses{ $hostname } } ) {
					print {$license_fh} "$hostname $license\n";
					}
				}
		}
}
joeDetroit is offline   Reply With Quote