Creating 1 file w/ the contents of 7 other files mixed randomly.

Z

Zachary

Say I've got 7 files ( file1.txt - file7.txt) and I want to combine the
lines from all 7 files into one file (file8.txt) but I want the line
order to be randomly mixed. In other words, I want file8.txt to
contain all the lines from file1.txt through file7.txt but in random
order so that each pass produces a different file8.txt. Might someone
help me accomplish this? Keep in mind that files 1-7 may not contain
the same number of lines. One may have a hundred lines wile the other
may only have ten.
 
P

Paul Lalli

Zachary said:
Say I've got 7 files ( file1.txt - file7.txt) and I want to combine the
lines from all 7 files into one file (file8.txt) but I want the line
order to be randomly mixed. In other words, I want file8.txt to
contain all the lines from file1.txt through file7.txt but in random
order so that each pass produces a different file8.txt. Might someone
help me accomplish this? Keep in mind that files 1-7 may not contain
the same number of lines. One may have a hundred lines wile the other
may only have ten.

What have you tried so far? How did it fail to meet your expectations?
What error messages did you recieve, or faulty output resulted?

Please post a short-but-complete script that demonstrates your errors.
Please also read the Posting Guidelines for similar good advice.

If you have not yet even made an attempt, please do so. If you have no
idea where to begin, please provide actual concrete questions about how
to accomplish the specific tasks that confuse you, rather than a
posting that amounts to "I want a program that does <foo>."

Paul Lalli
 
Z

Zachary

Why did you even reply? I work at UPS and I'm attemting to change
positions. The new position requires that I learn a ton of zip codes
so that I'll know where to send the packages in the buiding so I
figured I would kill 2 birds with one stone ( do some programming and
learn the zips ). I'm working on an application that I can use to test
myself. I'm also currently working through the "Learning Perl" and
"Programming Perl" books. It's just a hobby. I'm not a student trying
to cheat I just don't know how to accomplish the task above. So far
I'm just randomly picking a filehandle, plucking a line off the top,
and comparing with my answer until I reach EOF on each filehandle. The
problem with that is nothing varies.

Sorry if posted to the wrong group but you should just pass it on by if
you don't know the answer. I'm sure someone will help eventually or I
might even figure it out on my own. If you want to be a teacher, make
that your profession. If you just want to be an *sshole then keep up
the good work.

Zachary
 
X

xhoster

Zachary said:
Say I've got 7 files ( file1.txt - file7.txt) and I want to combine the
lines from all 7 files into one file (file8.txt) but I want the line
order to be randomly mixed. In other words, I want file8.txt to
contain all the lines from file1.txt through file7.txt but in random
order so that each pass produces a different file8.txt. Might someone
help me accomplish this?

If all the data fits in memory, then read all the data (for example,
"cat file[1-7].txt |"), shuffle it (List::Util) and write it back out.

If not, add a column containing a random number, sort (using
OS tools) on this column, and remove the extra column.
Keep in mind that files 1-7 may not contain
the same number of lines. One may have a hundred lines wile the other
may only have ten.

No problem.

Xho
 
E

Eric Schwartz

Zachary said:
Why did you even reply?

I'm not Paul, but I can answer this one: to help you ask questions
that will get answers. We have Posting Guidelines (which are posted
here regularly) for a reason: following them will get you more answers
of higher quality, to you questions. Paul stated only a few of the
more important ones.
So far I'm just randomly picking a filehandle, plucking a line off
the top, and comparing with my answer until I reach EOF on each
filehandle. The problem with that is nothing varies.

Show us some code, please. We could make all sorts of completely
impractical suggestions if we don't know what your code looks like.
If you actually give us some, we will not only be able to come up with
the best solution given what you have already, we will also be able to
show you where you went wrong. Of course, we don't really need to see
all 1500 lines of your program, so please isolate it into a small
program that we can paste into an editor and run ourselves.
Sorry if posted to the wrong group but you should just pass it on by if
you don't know the answer.

I assure you, Paul does in fact know the answer, or at least, *an*
answer. So do I, for that matter. But, while neither of us is keen
to see CLPM turn into an unpaid consultancy, we are more than happy to
help someone who has demonstrated that they have actually made some
effort on their own. Also, I'm not particularly keen to take the
somewhat ambiguous text description you gave, and try to turn it into
code. Remember-- you're the one with the problem; it's your job to
present it in such a way that it's as easy as possible for us to help
you with it.
I'm sure someone will help eventually or I
might even figure it out on my own. If you want to be a teacher, make
that your profession. If you just want to be an *sshole then keep up
the good work.

If you want to be ignored by the people who are best able to help you,
by all means, continue to call them "*sshole"s. If you want help from
smart people who will be able to help you become a better Perl
programmer, then read the Posting Guidelines, and follow them.

-=Eric
 
D

David Lee Lambert

Zachary said:
Say I've got 7 files ( file1.txt - file7.txt) and I want to combine the
lines from all 7 files into one file (file8.txt) but I want the line
order to be randomly mixed. In other words, I want file8.txt to
contain all the lines from file1.txt through file7.txt but in random
order so that each pass produces a different file8.txt. Might someone
help me accomplish this? Keep in mind that files 1-7 may not contain
the same number of lines. One may have a hundred lines wile the other
may only have ten.

something like this should work...

#/usr/bin/perl -w

@files = qw( file1.txt file2.txt file3.txt ); # etc.
open OUT,">file8.txt";

@lines = ();

for (@files) {
open F,$_;
while (<F>) { push @lines,$_ }
};

while ($#lines > -1) {
$i = int(rand($#lines+1));
($_) = splice(@lines,$i,1);
print OUT;
};
 
A

Anno Siegel

Zachary said:
Why did you even reply?

To give you a chance to make a posting that shows some code we can help
you with.
I work at UPS and I'm attemting to change
positions. The new position requires that I learn a ton of zip codes
so that I'll know where to send the packages in the buiding so I
figured I would kill 2 birds with one stone ( do some programming and
learn the zips ). I'm working on an application that I can use to test
myself. I'm also currently working through the "Learning Perl" and
"Programming Perl" books. It's just a hobby. I'm not a student trying
to cheat I just don't know how to accomplish the task above.

That is all mildly interesting, but does nothing to validate your
question. We help people correct their Perl programs. You are in
a stage of problem solving that isn't (yet) language-specific. That you
intend to implement a solution in Perl doesn't make it a Perl problem.
So far
I'm just randomly picking a filehandle, plucking a line off the top,
and comparing with my answer until I reach EOF on each filehandle. The
problem with that is nothing varies.

Read all the files in a big array. Select a random line from the
array each time through. Seems like that's all there's to it.
Sorry if posted to the wrong group but you should just pass it on by if
you don't know the answer.

It's not a question of knowing. Many of us earn their money by writing
Perl programs to specification. It would be bad for the market if we
did it here for free.
I'm sure someone will help eventually or I
might even figure it out on my own. If you want to be a teacher, make
that your profession. If you just want to be an *sshole then keep up
the good work.

You seem to be of the insult-them-until-they-help-me school of belief.
They don't last long...

Anno
 
R

robic0

Say I've got 7 files ( file1.txt - file7.txt) and I want to combine the
lines from all 7 files into one file (file8.txt) but I want the line
order to be randomly mixed. In other words, I want file8.txt to
contain all the lines from file1.txt through file7.txt but in random
order so that each pass produces a different file8.txt. Might someone
help me accomplish this? Keep in mind that files 1-7 may not contain
the same number of lines. One may have a hundred lines wile the other
may only have ten.

This implies making infinite file8's. It also implies that you can
bring all of files 1-7 into memory before writing file8.
If you can do that why make random files, just do it in memory.

Combine all the files into 1 sequentially then never change it.
In a loop, read all the lines into an array. Create a random
function that only works on the bounds of the array element numbers.
Call it to get the next random index. Do whaterver you want with each string....
Jeezzzzzzzzz
 
R

robic0

[top post for bozo:]
Please don't attach vcf's or anything else on this newsgroup!!!!!!!!
I see a greenie in Agent and it just drives me nuts.......
 
P

Paul Lalli

Zachary said:
Why did you even reply?

On the miniscule hope that you might have actually wanted *help*, as
you stated, instead of someone to write your program for you. It is
disheartening to see that I was wrong.
I work at UPS and I'm attemting to change
positions. The new position requires that I learn a ton of zip codes
so that I'll know where to send the packages in the buiding so I
figured I would kill 2 birds with one stone ( do some programming and
learn the zips ). I'm working on an application that I can use to test
myself.

I'm confused as to the relevance of any of this. None of it transforms
this newsgroup from one that assists people in writing and debugging
their programs to one that writes code for you.
I'm also currently working through the "Learning Perl" and
"Programming Perl" books.

Excellent. Good for you. Seriously.
It's just a hobby.

uh-huh.... and so when you have a hobby, you don't actually *try to
accomplish* anything relating to that hobby? You just tell people you
want something done until they do it for you? If carpentry were your
hobby, would you go around asking people to build you a birdhouse? Or
would you try to build a birdhouse and then ask for assistance in the
proper usage of a hammer?
I'm not a student trying to cheat

Well, good, because bluntly asking people to do your work for you would
make you *really* bad at cheating.
I just don't know how to accomplish the task above.

Right. Which is why I very specifically asked you to tell us what your
best guess was, to show us that you'd actually *attempted* to solve
your problem on your own, or to at the very least tell us what specific
parts were confusing you.
So far
I'm just randomly picking a filehandle, plucking a line off the top,
and comparing with my answer

What is "your answer"? You never mentioned anything about an "answer"
in your original post.
until I reach EOF on each filehandle. The
problem with that is nothing varies.

If you actually are "randomly" picking a filehandle, as you said, than
absolutely something varies. One of your last two statements is in
error. Without any code (have you read the Posting Guidelines yet,
also as I suggested?) it's impossible to know which one it is.
Sorry if posted to the wrong group

That depends on what you want. If you want someone to write code for
you, yes you posted to the wrong group (try jobs.perl.org - you may
have better luck there). If you want assistance writing/debugging your
own code, you're in the right place.
but you should just pass it on by if you don't know the answer.

This actually made me laugh. Thank you. What part of my post in any
way implied that *I* didn't know the answer? Or is this some bizarre
reverse psychology intended to get me to show my superiority by posting
the answer for you?
I'm sure someone will help eventually

Yes. "Someone" already has. I did. I think what you mean is "I'm
sure someone will write the program for me eventually." And frankly, I
think that's a distinct possibility. More's the pity.
or I might even figure it out on my own.

And if that happens, I will be among the first to congratulate you.
If you want to be a teacher, make that your profession.

As a matter of fact, it is. Thanks for noticing.
If you just want to be an *sshole then keep up the good work.

Oh, I don't need your encouragement for that. It comes pretty
naturally.


Your main problem with my response is in the arrogant belief that I
responded for your benefit and yours alone. I did not. I posted my
response to remind all future posters reading the archives of this
group that one should always make an attempt to help themselves before
asking thousands of people around the world to do it for them.

Fare thee well,
Paul Lalli
 
P

Paul Lalli

robic0 said:
In a loop, read all the lines into an array.

Why would you ever do that? Why not simply read the file into an array
directly, by using the readline operator in list context? If you've
already resigned yourself to storing the entire file(s) in memory,
seems pointless to spend cycles looping through the file...

Paul Lalli
 
R

robic0

To give you a chance to make a posting that shows some code we can help
you with.


That is all mildly interesting, but does nothing to validate your
question. We help people correct their Perl programs. You are in
a stage of problem solving that isn't (yet) language-specific. That you
intend to implement a solution in Perl doesn't make it a Perl problem.


Read all the files in a big array. Select a random line from the
array each time through. Seems like that's all there's to it.


It's not a question of knowing. Many of us earn their money by writing
Perl programs to specification. It would be bad for the market if we
did it here for free.


You seem to be of the insult-them-until-they-help-me school of belief.
They don't last long...

Anno

The OP made a half-hearted effort, couldn't quite cover his *I don't really
know what I'm trying to do*. When he's serious, that kind of aggresion will
turn to accertiveness and surely help him. Your response will definetly push
him in the right direction technically. Just remember Anno, your producing
another Charles Manson programming god...
 
R

robic0

Why would you ever do that? Why not simply read the file into an array
directly, by using the readline operator in list context? If you've
already resigned yourself to storing the entire file(s) in memory,
seems pointless to spend cycles looping through the file...

Paul Lalli

Yeh, thats what I meant. I thought this was an original post, didn't know it was
a thread. Didn't think he was technical enough to understand, or anyone else would respond.
My bad......
 
U

usenet

Zachary said:
[a question]

This question was multi-posted to perl.beginners (and maybe other
groups). That shows remarkable lack of nettiquite (or deliberate
rudeness on the part of the OP).
 
H

Harry

Zachary said:
Say I've got 7 files ( file1.txt - file7.txt) and I want to combine the
lines from all 7 files into one file (file8.txt) but I want the line
order to be randomly mixed. In other words, I want file8.txt to
contain all the lines from file1.txt through file7.txt but in random
order so that each pass produces a different file8.txt. Might someone
help me accomplish this? Keep in mind that files 1-7 may not contain
the same number of lines. One may have a hundred lines wile the other
may only have ten.

Don't re-invent the wheel :)

Google "randomize text file" will point you to here, which
co-incidently, is a Perl script.

Random Text Version 1.0
http://www.rahoorkhuit.net/programming/cgi/text/rand_text/index.shtml
 
R

robic0

Wow, you must be a brave little coder, posting a Perl4 Matt Wright script
here. And one that only prints a single line from a file instead of
randomising the entire file. :)


John

You know, actually you could cat all the files into one (no matter how big).
Given the number of lines in the file, using Tie::File or something, you
could get a randome number between 0..last line, then print it out.
I guess..
 
R

robic0

You know, actually you could cat all the files into one (no matter how big).
Given the number of lines in the file, using Tie::File or something, you
could get a randome number between 0..last line, then print it out.
I guess..

But hey, the file cat wouldn't seem to be the point (as the subject suggests).
Though for efficiency, why cat files? A random file(/lines) ([filex,lines]) index
is obtained, then the line index is randomized. Does randomize know file numbers?
How many files you need for a good statistical spread? The more the better.
Then Tie::File, rinse, repeat...
 

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
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top