perlnewbie9292
Programmer
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.
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.
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.