#!/usr/bin/perl use strict; use Tie::File; use List::Util qw/shuffle/;

You should also include the warnings pragma (it would have warned you about one of the lines of your program.)

#!/usr/bin/perl use warnings; 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);

Why are these lines in file scope?    They should be inside the subroutine.    You should also verify that open worked correctly.



&readfile();

There is no need to prepend an ampersand when using subroutine calls and it may cause unwanted side effects.



tie @array, 'Tie::File', $numberFile || die("failed to open $numbe +rFile");;

You should declare @array here where you first use it instead of at file scope.

You are using the high precedence || operator which means that you are testing the boolean value of $numberFile NOT the success or failure of tie.

tie my @array, 'Tie::File', $numberFile or die "failed to open $nu +mberFile";


if (-e $tempFile) { open(TEMPFILE,"+<$tempFile") || die("failed to open $tempFile" +);

There is no need to stat the file first, just let open do its job:

if ( open TEMPFILE, '+<', $tempFile ) {


my $counter=0;

You never use this variable anywhere??



tell(TEMPFILE);

You are using tell in void context so this line is superfluous.    (The warnings pragma would have warned you of this.)



while ($line = <CHARFILE>) {

You should declare $line here where you first use it instead of at file scope:

while ( my $line = <CHARFILE> ) {


if ("$line $array[$i]") {

This test is superfluous because the string "$line $array[$i]" is ALWAYS true.



foreach ( $line ) {

You are looping over a list with one element.    The foreach loop aliases $line to $_ but you are not using $_ inside the loop so it is superfluous.



print shuffle $array[$i] . "\n";

shuffle works on a list and returns a shuffled list but you are only passing shuffle a single element, the string "$array[$i]\n", so there is nothing really for shuffle to do, except return that one element.



print "\n" . $line . " " . rand "$array[$i]" . + "\n";

You have three problems there: 1) rand works with numbers but you are copying the contents of $array[$i] to a string, so perl has to convert that string to a number for rand to work, and: 2) because of the high precedence of the concatenation operator you are actually passing rand the string "$array[$i]\n" which means that: 3) the newline is not printed out as you may have expected.

That will work correctly if you use the comma operator instead:

print "\n", $line, " ", rand $array[ $i ], "\n +";


You should also include the $! in your error messages when using open so you know why it failed.


In reply to Re: Hash shuffle help by jwkrahn
in thread Hash shuffle help by perlnewbie9292

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.