in reply to RE: Random and sequential for the price of one!
in thread Reading files sequentially
Warning, in that I didn't test that code, I only thought about it, and ran it through the syntax checker. Hopefully that is clearer and will help you out!sub shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } } my $counter = 10; # this is the number of images you want open DAT, "ads.dat" or die "Error: cannot open data file!"; my @item; while (my $c=<DAT>) { my ($id,$image,$url)=split(/\|/,$c); push @item, {"image"=>$image,"url"=>$url,"desc"=>$desc}; } close DAT; while ($counter) { my @itemtmp = @item; shuffle(\@itemtmp); while (my $i = pop @itemtmp) { $img = $i->{"image"}; $link = $i->{"url"}; print qq|Set-Cookie: URL=$link\n|; print qq|Location: $img\n\n|; last if --$counter == 0; } }
Ciao,
Gryn
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Sample Random (Code)
by damian (Beadle) on Sep 15, 2000 at 08:05 UTC | |
by gryng (Hermit) on Sep 15, 2000 at 08:40 UTC | |
by damian (Beadle) on Sep 15, 2000 at 09:09 UTC | |
by tilly (Archbishop) on Sep 15, 2000 at 12:47 UTC |