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

Hashes help.

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
0
0
US
Hello all,

I am kinda stuck with what I am trying to do. Here is my code. I am creating a subroutine, which will be going into a larger script. Where I am stuck is trying to figure out the best to tackle to following problem.

Code:
#!/usr/bin/perl
use strict;
use Tie::File;

my $line;
my $i=0;
my @array;
my $count=0;
my $fileA = 'sampleFile.txt';
my $fileB = 'sampleFile2.txt';
my $fileCounter = 'counter.txt';
my $tempFile = 'temp.txt';

open(FL,$fileCounter);
$count= <FL>;
chomp($count);

&readfile();

sub readfile {
	tie @array, 'Tie::File', $fileB || die("failed to open $fileB");;
	my %hashData;
	open(FL2, $fileA) || die("failed to open $fileA");

	if ($count == 5) {
		$count=0;
		unlink($tempFile);
	} else {
		$count++;
	}
	open(FL,">$fileCounter") || die("failed to open $fileCounter");
	print FL $count;
	close(FL);
	if (-e $tempFile) {
		open(FL3,"+<$tempFile") || die("failed to open $tempFile");
		while (<FL3>) {
			chomp;
			if ($_) {
				$_ =~ s/\s+//;
				print "--->>>>$_\n";
				$hashData{$_} = 1;
			}
		}
	} else {
		open(FL3,">>$tempFile") || die("failed to open $tempFile");
	}
	my $counter=0;
	tell(FL3);
	while ($line = <FL2>) {
		chomp($line);
		if (exists $hashData{"$line$array[$i]"}) {
			next;
		} 
		else {
			$hashData{"$line$array[$i]"} = 1;
			print FL3 "$line $array[$i]\n";
			print "$line $array[$i]\n"; 
		}
		$i++;
	}
	print FL3 "\n\n";
	close(FL3);
	close(FL2);
	untie @array;
}

Contents of sample files:
fileA: has values 1-5, with 1 value per line
fileB: has values a-m, with 1 character per line.

Now what I need the code to do is the follwoing:
print the data from fileB with any of the numbers in fileA (random is fine, order does not really matter).

Once it print to stdout, it should also keep track of what was printed. Which is where the tempFile comes in.

Now the next time the script is run. It should check the tempFile before print out the data to stdout. It should check to make sure that the combinations that it is now printing have not occurred during the last run. It does this by checking the data stored in the tempFile. It will keep track of the last five times the script has been run, and what was print. And during those five times the script should not print any of the same combinations.

for example:
RUN1 output
a 1
b 2
c 3
etc...

RUN2 output
a 3
b 5
c 1
etc...

RUN3 output
a 5
b 1
c 4
etc..

Each time the script runs
it should not print the same combination of data. So it should check the tempFile, where it stores each run and shuffle/rand the data until is unique. Trying to keep state of the combinations that were printed for 5 runs.

Any help pointers would be greatly appreciated. I think i've been playing with this for so long that i need a second pair of eyes.

Thanks for all the help.
 
Well, it seems to be working, more-or-less. I'd give the file handles better names than FL, FL1, FL2, etc. (I had to do this to make sense of the code).

I think the only thing you need to do now is change this part:

Perl:
        if (exists $hashData{"$line$array[$i]"}) {
            next;
        }

So that instead of just doing a next it actually tries looping through the available numbers looking for one that hasn't been used before.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top