Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 33
  1. #16
    New Coder
    Join Date
    Nov 2011
    Location
    Detroit
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That makes sense... Thanks for the explanation :-)

    I was in the middle of posting the output and was pulled away...

    Here's what your code (run as-is) produced:
    Code:
     'WPDONCND119BLK8' => [
                            '1111-1111-1111-1111'
                          ],
     'WPDONCND11528CH' => [
                            '2222-2222-2222-2222'
                          ],
     'UNASSIGNED' => [
                       '1111-1111-1111-1111',
                       '2222-2222-2222-2222',
                          ] ....
    I like how all the UNASSIGNED keyes are in one chunk... easy to see (and I found a duplicate thanks to your code)

  2. #17
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Good to hear that you like results.

    Let me know if you want to keep it this way or not.

  3. #18
    Banned
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your substitution regex is using $ENV{'ComputerName'} and implies that the script will be run on the laptop instead of the deployment server, which doesn't make sense. Where is the laptop hostname coming from?
    Last edited by WA; 11-17-2011 at 02:29 AM.

  4. #19
    New Coder
    Join Date
    Nov 2011
    Location
    Detroit
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This definitely helps, especially as it lets me see duplicates easily.

    Ideally, I'd like to be able to assign one of the licenses marked 'UNASSIGNED' to the machine that I'm building, then either A) change the one line of the original file, or B) use the move function of File::Copy to move the original file to be the backup and write out the hash (with the change) into a new file.

    For example:
    Code:
    UNASSIGNED 1111-1111-1111-1111
    becomes
    Code:
    WPDONCND12038CJ7 1111-1111-1111-1111
    That's what I was trying to do with my original code:
    Code:
    while ($setCount == 0){
    	if ($line =~ m/$strSearchFor/){ #this line would look for the word UNASSIGNED
    	$line =~ s/$strSearchFor/$ENV{'ComputerName'}/i; #this line would replacethe word UNASSIGNED with the current machine name
    	$setCount = 1;
    	print LICENSE ($line); 
    		}
    but I'm not sure how to get there from here...

  5. #20
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Once you load the "license" file into the hash, you would add a new key (which is the hostname of the laptop) and its value is taken/extracted from one of the values of the anonymous array of the "unassigned" key.

    For example:
    Code:
    $licenses{ $ENV{'COMPUTERNAME'} } = shift @{ $licenses{ 'UNASSIGNED' } };
    After that, you rewrite the "license" file with the data in the %licenses hash.
    Last edited by FishMonger; 11-18-2011 at 03:36 AM.

  6. #21
    New Coder
    Join Date
    Nov 2011
    Location
    Detroit
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by FishMonger View Post
    Once you load the "license" file into the hash, you would add a new key (which is the hostname of the laptop) and its value is taken/extracted from one of the values of the anonymous array of the "unassigned" key.

    For example:
    Code:
    $licenses{ $ENV{'COMPUTERNAME'} } = shift @{ $licenses{ 'UNASSIGNED' } };
    After that, you rewrite the "license" file with the data in the %licenses hash.
    Wow... talk about jumping into the deep end...

    I'll give that a shot at work when I'm more awake.

    ---EDIT: I reserve the right to ask more dumb (perhaps obvious to seasoned coders) questions :-D

    Thank you for your help!
    Joe

  7. #22
    New Coder
    Join Date
    Nov 2011
    Location
    Detroit
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  8. #23
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Code:
    if ( {keys %licenses} =~ m/$strSearchFor/i){
    That's not how to test if a hash key exists. You need to use the exists function.

    Code:
    if ( exits $licenses{$strSearchFor} ) {
    In your prior posts, the "hostname" field was always in uppercase. If that's not the case, then you should force the case (to either upper or lower) when assigning the hash.

  9. #24
    New Coder
    Join Date
    Nov 2011
    Location
    Detroit
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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);
    		}
    }

  10. #25
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    %licenses is a Hash-of-Arrays so you'll need to use a nested loop to dereference the array.

    Also, why are you not taking my advise about the proper/best way to open a filehandle?

    Code:
    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";
        }
    }

  11. #26
    New Coder
    Join Date
    Nov 2011
    Location
    Detroit
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by FishMonger View Post
    Also, why are you not taking my advise about the proper/best way to open a filehandle?
    I had that in there and hadn't changed it yet, that's all. I wanted to get it working then clean it up (remove all the <STDIN> 'breakpoints' so I knew where I was, etc.)

    I'll change it right now.

    One thing though- since open my $license_fh was used to open the original license file, can I re-use it to open the new license file or should I use something like open my $newlicense_fh

    I'm just wondering what the rules are for that...
    Thanks again for your help!
    Last edited by joeDetroit; 12-06-2011 at 03:50 PM.

  12. #27
    New Coder
    Join Date
    Nov 2011
    Location
    Detroit
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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";
    					}
    				}
    		}
    }

  13. #28
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Change:
    Code:
    $licenses{ $ENV{'COMPUTERNAME'} } = shift @{ $licenses{ 'unassigned' } };
    To this:
    Code:
    $licenses{ $ENV{'COMPUTERNAME'} } = [ shift @{ $licenses{ 'unassigned' } } ];

  14. #29
    New Coder
    Join Date
    Nov 2011
    Location
    Detroit
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That worked...

    Do the square brackets create a reference?

  15. #30
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Yes, they created the required array reference.


 
Page 2 of 3 FirstFirst 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •