Help me randomize a record rewrite

A

asm_toronto

Hi,
I am wondering if someone can direct me on how to randomize the records
as I rewrite them. Hope to catch someone in a good mood to help an
eternal
amateur.

Al i want to do is to take an existing datafile with 100 records, and
rewrite them
in random order. (Each only has one field.)

Here is what I have:
open (DATA, "$BaseDir/my_db.txt");
@Ads = <DATA>;
close DATA;

open (CREATEFILE, ">$BaseDir/mydb_new.txt");
close (CREATEFILE);

foreach $Ad (@Ads) {
@Field = split (/\|/, $Ad);

open (LIST, ">>$BaseDir/agentdb_new.txt");
print LIST "$Field[1]\n";
close LIST;


}
 
E

Eric Bohlman

Al i want to do is to take an existing datafile with 100 records, and
rewrite them
in random order. (Each only has one field.)

Here is what I have:

Here is what you don't have, but need to have:

use strict;
use warnings;
open (DATA, "$BaseDir/my_db.txt");

*Always* check to see whether or not an attempt to open a file was
successful, and don't plow ahead mindlessly if it wasn't.
@Ads = <DATA>;
close DATA;

open (CREATEFILE, ">$BaseDir/mydb_new.txt");
close (CREATEFILE);

What on earth is this for?
foreach $Ad (@Ads) {
@Field = split (/\|/, $Ad);

Please indent code in blocks.
open (LIST, ">>$BaseDir/agentdb_new.txt");

Open the file (for writing, not appending) *once* before the loop, rather
than opening and closing it for each line output.
print LIST "$Field[1]\n";

You said each record had only one field, but here you're trying to output
the second field...
close LIST;


}

Once you've made all these corrections, type

perldoc -q shuffle

at your command prompt and insert code similar to what you'll see there
in between reading the array and writing it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top