Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Creating 1 file w/ the contents of 7 other files mixed randomly.

Reply
Thread Tools

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

 
 
game4itguy@gmail.com
Guest
Posts: n/a
 
      04-19-2006
Arguments aside (it's awfully dull reading the same rants over and
over), this script will do what you want if you place it in the
directory of the original text files. It should be noted that on my
Windows machine file8.txt was not overwritten with each run of the
script.

#!/usr/bin/perl -w
use strict;
my $rand;
my @indir;
my @files;
opendir(DH,".") || die $!;
@indir = readdir(DH);
close(DH);
foreach(@indir) {
if ($_ =~ m/.txt/) {
open(FH,$_);
push(@files, <FH>);
close(FH);
}
}
open(SAVETO,">file8.txt") || die $!;
while($#files > 0)
{
$rand = int(rand(@files));
print SAVETO $files[$rand];
delete $files[$rand];
}
close SAVETO;
exit;

As a novice I'd be interested in receiving a critique of this script
and how I could make it better/things I've done wrong, if anyone's
interested in providing one? Although whilst I'm aware it will
undoubtedly get torn to shreds by the pros, I must say I'm rather
pleased that it works at all!

 
Reply With Quote
 
 
 
 
robic0
Guest
Posts: n/a
 
      04-19-2006
On 18 Apr 2006 18:27:19 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>Arguments aside (it's awfully dull reading the same rants over and
>over), this script will do what you want if you place it in the
>directory of the original text files. It should be noted that on my
>Windows machine file8.txt was not overwritten with each run of the
>script.
>
>#!/usr/bin/perl -w
>use strict;
>my $rand;
>my @indir;
>my @files;
>opendir(DH,".") || die $!;
>@indir = readdir(DH);
>close(DH);
>foreach(@indir) {
>if ($_ =~ m/.txt/) {
>open(FH,$_);
>push(@files, <FH>);
>close(FH);
>}
>}
>open(SAVETO,">file8.txt") || die $!;
>while($#files > 0)
>{
>$rand = int(rand(@files));
>print SAVETO $files[$rand];
>delete $files[$rand];
>}
>close SAVETO;
>exit;
>
>As a novice I'd be interested in receiving a critique of this script
>and how I could make it better/things I've done wrong, if anyone's
>interested in providing one? Although whilst I'm aware it will
>undoubtedly get torn to shreds by the pros, I must say I'm rather
>pleased that it works at all!


I haven't even read it, but I like your attitude! You take that step
knowing there may be no floor to land on.

You have the power Luke, practice your saber..
The force is with you young one
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-19-2006
Zachary <(E-Mail Removed)> wrote:

> I want the line
> order to be randomly mixed.



perldoc -q random


> Might someone
> help me accomplish this?



That would be much easier for us to do if you told us what part
it is that you are stuck on.

What part are you stuck on?

Got any code?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Zachary
Guest
Posts: n/a
 
      04-19-2006
I am so very sorry if I've wasted anyones time. I obviously should
have read more posts in an attempt to better understand the conventions
use to post to this newsgroup. I'm a newbie and while that doesn't
excuse my actions I hope you all will understand so that in the future
you don't just simply pass up my posts when you see my name. I truly
didn't intend to seem slack (get it? Slack Zack). I was simply trying
to state my problem as briefly as possible without going into detail
about how the entire program works. The problem is just a portion of
the program. I would never try to get any of you to work for free and
I certainly didn't intend to waste any of your valuable time. I
promise that I value your time as much as anyone else does. For those
of you that replied with helpful info, I thank you very much and I will
surely make every attempt not to waste your time in the future.

As for Paul Lalli, if you spend that much time correcting every post
you see unfit then you need a woman. You need to go dancing, have a
beer or anything that'll allow you some release. Put the newsgroups
down for awhile dog.

I'll come back when I learn a little more about "netiquette."
Jeeez! I got a lot of new words to learn too.

Thanks,
Zachary

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-19-2006
>>>>> "g" == game4itguy <(E-Mail Removed)> writes:

g> Arguments aside (it's awfully dull reading the same rants over and
g> over), this script will do what you want if you place it in the
g> directory of the original text files. It should be noted that on my
g> Windows machine file8.txt was not overwritten with each run of the
g> script.

and rants aside your program is amazingly buggy and poorly written.

g> #!/usr/bin/perl -w
g> use strict;
g> my $rand;
g> my @indir;
g> my @files;

don't declare those until they are used.

g> opendir(DH,".") || die $!;

use lexical handles as they are safer.

g> @indir = readdir(DH);
g> close(DH);
g> foreach(@indir) {
g> if ($_ =~ m/.txt/) {

no need for $_.

g> open(FH,$_);

no check for open failure.

g> push(@files, <FH>);

oooh! slurping. maybe you should look into file::slurp?

and that array contains all the lines in the files, not the files
themselves. a better name is needed for it.
g> close(FH);
g> }
g> }

ever heard of indenting?

g> open(SAVETO,">file8.txt") || die $!;
g> while($#files > 0)
g> {
g> $rand = int(rand(@files));
g> print SAVETO $files[$rand];
g> delete $files[$rand];

here is your big bug. that doesn't do what you think it does. have you
run this code? my turing machine says it will never stop.

g> }
g> close SAVETO;
g> exit;

g> As a novice I'd be interested in receiving a critique of this script
g> and how I could make it better/things I've done wrong, if anyone's
g> interested in providing one? Although whilst I'm aware it will
g> undoubtedly get torn to shreds by the pros, I must say I'm rather
g> pleased that it works at all!

are you sure it works? delete doens't remove an element from an array,
just the value. $#files will not change.

perl -le '@a = (1 .. 3); print $#a ; delete $a[1]; print $#a'
2
2

try this on for size (untested):

use File::Slurp ;
use List::Util qw( shuffle ) ;

write_file( 'file8.txt', shuffle
map read_file( "file$_.txt" ), 1 .. 7
) ;

kinda cute, ain't it? clear, fast, no variables (other than $_).

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      04-19-2006
Uri Guttman <(E-Mail Removed)> wrote:

> oooh! slurping. maybe you should look into file::slurp?


File::Slurp you mean (file::slurp works on Windows, but fails on
CaSEsEnsitIVe filesystems.

--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-19-2006
John Bokma <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Uri Guttman <(E-Mail Removed)> wrote:
>
> > oooh! slurping. maybe you should look into file::slurp?

>
> File::Slurp you mean (file::slurp works on Windows,


....for small values of "works". It will load the module but not
import anything from it.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      04-19-2006
Zachary wrote:
> I am so very sorry if I've wasted anyones time. I obviously should
> have read more posts in an attempt to better understand the conventions
> use to post to this newsgroup.


And you *still* haven't read the Posting Guidelines for this group, as
evidenced by your very rude lack of quoting any context in your
replies.

> I'm a newbie and while that doesn't
> excuse my actions I hope you all will understand so that in the future
> you don't just simply pass up my posts when you see my name.


After I read this paragraph, I thought "Okay, he apologized, I'm
willing to help him again."

> As for Paul Lalli, if you spend that much time correcting every post
> you see unfit then you need a woman. You need to go dancing, have a
> beer or anything that'll allow you some release. Put the newsgroups
> down for awhile dog.


..... and then I read this. This is how you show how mature you are,
and how you ask the newsgroup to continue to help you despite the poor
first impressions? By insulting the people trying to help you? Good
luck getting any help out of this newsgroup. It will be an uphill
battle, I'm sure, as you've most likely just landed in the killfiles of
many of the most helpful people in this newsgroup.

> I'll come back when I learn a little more about "netiquette."
> Jeeez! I got a lot of new words to learn too.


Not to mention manners.

Paul Lalli

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-19-2006
Zachary <(E-Mail Removed)> wrote:

> I am so very sorry if I've wasted anyones time.



I do not believe you, given what you wrote below.


> I obviously should
> have read more posts in an attempt to better understand the conventions
> use to post to this newsgroup. I'm a newbie and while that doesn't
> excuse my actions I hope you all will understand so that in the future
> you don't just simply pass up my posts when you see my name.



Too late...


> As for Paul Lalli, if you spend that much time correcting every post
> you see unfit then you need a woman. You need to go dancing, have a
> beer or anything that'll allow you some release. Put the newsgroups
> down for awhile dog.



.... because you felt the need to include that.

Each component of Paul's followup would have helped you to
accomplish your goal.


> I'll come back when I learn a little more about "netiquette."



And you will be invisible when you do. So long!

*plonk*


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-19-2006
Zachary schreef:

> if you spend that much time correcting every post
> you see unfit then you need a woman.


You're leaving. Please don't forget to close the door behind you, we
don't need no creeps here.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding contents on yaml file without overwriting actual contents Kamarulnizam Rahim Ruby 4 01-28-2011 09:10 AM
if innerHTML used twice then it replaces the contents i want it to display all the contents virendra.amritkar@gmail.com Javascript 0 06-29-2007 08:13 AM
particular costraints in mixed contents JU XML 2 07-03-2006 01:25 AM
Creating a File object out of the contents of jar/zip file kotauk@gmail.com Java 5 04-06-2006 08:36 PM
Can I restrict both attribute contents and element contents in schema Don Adams XML 1 03-05-2004 12:48 PM



Advertisments