perlnewbie9292 has asked for the wisdom of the Perl Monks concerning the following question:
I am working with two sample files right now, just to get thing working.#!/usr/bin/perl use strict; use Tie::File; use List::Util qw/shuffle/; my $line; my $i=0; my @array; my $count=0; my $alphaFile = 'alphabetFile.txt'; my $numberFile = 'numberFile.txt'; my $fileCounter = 'counter.txt'; my $tempFile = 'temp.txt'; open(FILECOUNTER,$fileCounter); $count = <FILECOUNTER>; chomp($count); &readfile(); sub readfile { tie @array, 'Tie::File', $numberFile || die("failed to open $numbe +rFile");; my %hashData; open(CHARFILE, $alphaFile) || die("failed to open $alphaFile"); if ($count == 5) { $count=0; unlink($tempFile); } else { $count++; } open(FILECOUNTER,">$fileCounter") || die("failed to open $fileCoun +ter"); print FILECOUNTER $count; close(FILECOUNTER); if (-e $tempFile) { open(TEMPFILE,"+<$tempFile") || die("failed to open $tempFile" +); while (<TEMPFILE>) { chomp; if ($_) { $_ =~ s/\s+//; $hashData{$_} = 1; } } } else { open(TEMPFILE,">>$tempFile") || die("failed to open $tempFile" +); } my $counter=0; tell(TEMPFILE); while ($line = <CHARFILE>) { chomp($line); if (exists $hashData{"$line$array[$i]"}) { next; } else { $hashData{"$line$array[$i]"} = 1; #print TEMPFILE "$line $array[$i]\n"; #print "$line $array[$i]\n"; if ("$line $array[$i]") { foreach ( $line ) { if ( $array[$i] ) { print $line . " "; print shuffle $array[$i] . "\n"; print TEMPFILE "$line $array[$i]\n" } else { print "\n" . $line . " " . rand "$array[$i]" . + "\n"; print TEMPFILE "$line $array[$i]\n" } } } } $i++; } print TEMPFILE "\n\n"; close(TEMPFILE); close(CHARFILE); untie @array; }
Each time the script runs it should not print the same combination of data. So it should, again, check the tempFile, where it stores each run and shuffle/rand the data until is unique (This is where I am having problems/Stuck).for example: RUN1 output a 1 b 2 c 3 skipping… l 3 m 1 RUN2 output a 3 b 5 c 1 skipping… l 2 m 4 RUN3 output a 5 b 1 c 4 skipping… l 1 m 5
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash shuffle help
by jwkrahn (Abbot) on Nov 15, 2011 at 04:40 UTC | |
by Anonymous Monk on Nov 15, 2011 at 05:15 UTC | |
|
Re: Hash shuffle help
by GrandFather (Saint) on Nov 15, 2011 at 00:43 UTC |