View Single Post
Old 12-06-2011, 02:41 PM   PM User | #24
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
So, I'm finally able to get back to the script...

The if (exists... worked great.

Now I'm trying to get the script's output to a file, and I've got some of it, but I have a question (as usual):

When I use
Code:
while (my ($hostname, $license) = each (%licenses)) {
			print NEWLICENSE ($hostname . " " . $license . "\n");
			}
to get the hash into a file, I get the folowing:
Code:
cpdon0005nzfkh1 ARRAY(0x2eebc8)
unassigned ARRAY(0x33b6d04)
dlmdy1qp ARRAY(0x2eea00)
WPDONCND1277PXF 1111-1111-1111-1111
from this data:
Code:
cpdon0005nzfkh1 1111-1111-1111-1111
dlmdy1qp 2222-2222-2222-2222
unassigned 1111-1111-1111-1111
unassigned 2222-2222-2222-2222
So, the data has 2 unassigned licenses, and the script finds the first one and assigns it (which is what's supposed to happen) but how do I get the actual data from the hash for the license keys as opposed to the array reference (that's the hex assignment of where the key is in the array, right?)

Here's my full code thus far:
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 (NEWLICENSE, ">$newFile") or die "sorry. $!";
			while (my ($hostname, $license) = each (%licenses)) {
			print NEWLICENSE ($hostname . " " . $license . "\n");
			}
			close (NEWLICENSE);
		}
}
joeDetroit is offline   Reply With Quote