CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
cant output through perl or shell

 



omega
Novice

Dec 15, 2012, 11:12 AM

Post #1 of 15 (2996 views)
cant output through perl or shell Can't Post

my program works find if i use STDOUT, but if i use any form of piping either in perl with the open, '>', 'random****.txt' or if i use the shell command perl myfile > some****.txt the file shows 615mib but when you open it its blank.

p.s. i apologize in advance for not using strict or warnings, my version is 5.014 - please bare that in mind and be kind =)


Code
#!/usr/bin/perl 

@final_countdown = 1..53;

foreach $fc ( sort @final_countdown ) {
if ( $fc =~ /\d\d/ ) {
# print "$fc\n";
$final_countdown{"$fc"} = 1;
} else {
# print "0$fc\n";
$final_countdown{"0$fc"} = 1;
}
# print "$fc\n";
}

foreach $fc1 ( sort keys %final_countdown ) {
# print "$fc1\n";
foreach $fc2 ( sort keys %final_countdown ) {
if ( "$fc1" ne "$fc2" ) {
if ( "$fc2" > "$fc1" ) {
# print "$fc1 - $fc2\n";
foreach $fc3 ( sort keys %final_countdown ) {
if ( "$fc2" ne "$fc3" ) {
if ( "$fc1" ne "$fc3" ) {
if ( "$fc3" > "$fc2" ) {
# print "$fc1 - $fc2 - $fc3\n";
foreach $fc4 ( sort keys %final_countdown ) {
if ( "$fc3" ne "$fc4" ) {
if ( "$fc2" ne "$fc4" ) {
if ( "$fc1" ne "$fc4" ) {
if ( "$fc4" > "$fc3" ) {
# print "$fc1 - $fc2 - $fc3 - $fc4\n";
foreach $fc5 ( sort keys %final_countdown ) {
if ( "$fc4" ne "$fc5" ) {
if ( "$fc3" ne "$fc5" ) {
if ( "$fc2" ne "$fc5" ) {
if ( "$fc1" ne "$fc5" ) {
if ( "$fc5" > "$fc4" ) {
# print "$fc1 - $fc2 - $fc3 - $fc4 - $fc5\n";
foreach $fc6 ( sort keys %final_countdown ) {
if ( "$fc5" ne "$fc6" ) {
if ( "$fc4" ne "$fc6" ) {
if ( "$fc3" ne "$fc6" ) {
if ( "$fc2" ne "$fc6" ) {
if ( "$fc1" ne "$fc6" ) {
if ( "$fc6" > "$fc5" ) {
print "$fc1 - $fc2 - $fc3 - $fc4 - $fc5 - $fc6\n";
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}


i removed the open FINAL, '>', countdown.txt from the top and the print FINAL portion to debug and see if i could: perl myprogam > countdown.txt instead, obv did not work =\


Laurent_R
Veteran / Moderator

Dec 15, 2012, 12:30 PM

Post #2 of 15 (2991 views)
Re: [omega] cant output through perl or shell [In reply to] Can't Post

First, on which OS are you using Perl? Redirecting output with the '>' sign might not work everywhere.

Second, if you don't put the command that fails, we can't help you and guess what might be wrong with it.

Third, all your print statements are commented out, that does not help figuring out what might be wrong, print statements are usually one of the quite efficient ways to debug a program.

Finally, writing things like

Code
 
if ( "$fc4" ne "$fc5" ) {
if ( "$fc3" ne "$fc5" ) {
if ( "$fc2" ne "$fc5" ) {
if ( "$fc1" ne "$fc5" ) {
if ( "$fc5" > "$fc4" ) {


is really not efficient coding (quotes are useless and nested if are difficult to read).

You could use something like:


Code
if ( $fc4 ne $fc5  and $fc3 ne $fc5 and ... )



Chris Charley
User

Dec 15, 2012, 12:48 PM

Post #3 of 15 (2988 views)
Re: [omega] cant output through perl or shell [In reply to] Can't Post


Code
@final_countdown = 1..53;

This could be written more simply to create the leading zero's.

Code
@final_countdown = '01' .. '53';



omega
Novice

Dec 15, 2012, 12:48 PM

Post #4 of 15 (2988 views)
Re: [Laurent_R] cant output through perl or shell [In reply to] Can't Post

in order...
1. backtrack 5
2. that was the problem, nothing is failing
3. the print statements were for debugging, the final print statement isnt '#' 'd

about it being difficult to read, i apologize, i was trying to write a quick and dirty script to print all possible combinations of 1-53 for a 6 slot entry without repeating any numbers.

on another site someone mentioned the issue might be the text editor not being able to read a multi-million line file which after making a quick:

Code
open COUNTDOWN, '<', 'countdown.txt'; 
while ( <COUNTDOWN> ) {
print "$_\n";
}

showed to be true. my issue now is that without being able to physically read the file or be able to load it into an array or hash, which i am unable to do because it overloads my ram, the output is unusable. Any ideas?

In Reply To



omega
Novice

Dec 15, 2012, 12:51 PM

Post #5 of 15 (2984 views)
Re: [Chris Charley] cant output through perl or shell [In reply to] Can't Post

thank you Chris =) I was wondering if there was an easier way to format numbers for sort to work properly.


Chris Charley
User

Dec 15, 2012, 1:09 PM

Post #6 of 15 (2982 views)
Re: [omega] cant output through perl or shell [In reply to] Can't Post

I haven't examined your code closely, but I thought the following numbers for permutations, (all possible orders of 6 numbers), and combinations, (all different sets of 6 numbers where their order in the set doesn't matter).

For 53 numbers, there are:

22,957,480 combinations C(53, 6)
16,529,385,600 permutations P(53, 6)

I'll try to divine your intent from your code :-)


omega
Novice

Dec 15, 2012, 1:15 PM

Post #7 of 15 (2979 views)
Re: [Chris Charley] cant output through perl or shell [In reply to] Can't Post

the order doesn't matter, it could be 6-3-4-5-2-1, they just cant repeat like 6-6-4-5-2-2. the sorting was purely aesthetic

p.s. - thank you so much for helping a n00b


Laurent_R
Veteran / Moderator

Dec 15, 2012, 2:31 PM

Post #8 of 15 (2971 views)
Re: [omega] cant output through perl or shell [In reply to] Can't Post


In Reply To
on another site someone mentioned the issue might be the text editor not being able to read a multi-million line file which after making a quick:

Code
open COUNTDOWN, '<', 'countdown.txt'; 
while ( <COUNTDOWN> ) {
print "$_\n";
}

showed to be true. my issue now is that without being able to physically read the file or be able to load it into an array or hash, which i am unable to do because it overloads my ram, the output is unusable. Any ideas?


Unless I missed something, you did not show anywhere in your original code any statement to open a file. How are we supposed to guess that?

Besides, I don't get that. Reading the file with an iterator as the syntax:


Code
open COUNTDOWN, '<', 'countdown.txt'; 
while ( <COUNTDOWN> ) {
print "$_\n";
}

indicates does not load the full file into memory, but just one line at a time. And, doing this, you should be able to read a file of just about any size. I have read files having sizes of several hundred gibabytes with such a method and never encountered any problem.

Well, actually, you might have a problem if the file being read does not have any record separators (carriage returns or end of line characters), meaning that the while loop cannot break the input into records and that you are then trying to put the whole file into the $_ variable, which might or course exceed the memory capacité of your platform.

So the problem might be with the file format. For example, one possible source of the problem: if you are working under Windows, the default input record separator will probably be a combination of two characters (ASC 13 and ASC 10, if I remember the order correctly), whereas if your file was generated under Unix the record separator will be only ASC 13 (new line or \n). In this case, your program might not be able to break up your input into records and might try to slurp the whole content of the file into $_. This Windows/Unix format issue is just one example, there could be other reasons why Perl if not recognizing individual records correctly.

In this case, there are various ways to preprocess your file to guarantee that the format is right, or you could modify explicitly the default input record separator (the $/ variable). Once this is correct, there should be absolutely no reason why you would not be able to read a file having trillions of lines (except or course that it might take quite a bit of time).

Well, tell us more about the file you are reading, we did not even know you were reading one.


omega
Novice

Dec 15, 2012, 3:30 PM

Post #9 of 15 (2969 views)
Re: [Laurent_R] cant output through perl or shell [In reply to] Can't Post

thank you Lauren for your help, i should have been more explicit when i said

Quote
i removed the open FINAL, '>', countdown.txt from the top and the print FINAL portion to debug and see if i could: perl myprogam > countdown.txt instead, obv did not work =\

or simply placed this back inside the file as to not confuse, my apologies.

the reason that i mentioned the open, '<', 'countdown.txt' was because someone on another forum suggested the file might be too long for the editors im using to read so i tested it with that brief program and a | more pipe on my shell.

my new problem is this:
if i cant physically read the program to view the entries and i cant load it into ram because it overloads it (in both hash and array), how can i load this information back into my program to make all this data useable without having to open the file and print it to another file with that then would require another open command?


Chris Charley
User

Dec 15, 2012, 6:10 PM

Post #10 of 15 (2950 views)
Re: [omega] cant output through perl or shell [In reply to] Can't Post


Quote
I was wondering if there was an easier way to format numbers for sort to work properly.


No need to format the numbers, use a numeric sort.


Laurent_R
Veteran / Moderator

Dec 16, 2012, 2:32 AM

Post #11 of 15 (2939 views)
Re: [omega] cant output through perl or shell [In reply to] Can't Post

Sorry, I had not seen the final remark after the code snippet on your original post

Since you are speaking of piping commands and of shell, I'll assume you are working on some brand of Unix or Linux.

A couple of questions about your file, to try to have a better diagnostic:
- if you issue a wc command on your file, what is the output?
- if you issue a 'head' command on your file, what is the size of the output? If the size is manageable, please redirect the output to a file and post the result.

And, yes, I am sure you will be able to physically read the file, there is absolutely no reason why if should be impossible, it might just require a slightly modified approach if the 'while (<$foo>) {...' command does not do what you need. It could be changing the input record separator (as already mentioned in my previous post), or using the 'read' function instead of the the "while (<...>)" construct, or something else. But I would like to know more about your file before giving any advice on how to best proceed, hence my questions above in this post.


FishMonger
Veteran / Moderator

Dec 16, 2012, 8:31 AM

Post #12 of 15 (2933 views)
Re: [omega] cant output through perl or shell [In reply to] Can't Post

You instructor won't accept this, but it's how I'd do it.


Code
#!/usr/bin/perl 

use v5.10.1;
use strict;
use warnings;
use Algorithm::Permute;
use List::Permutor;

my @numbers = '01'..'04'; # extend the list as needed

# method #1
Algorithm::Permute::permute { say join('-', @numbers) } @numbers;

print "\n" x 3;

# method #2
my $perm = List::Permutor->new(@numbers);
while (my @number_set = $perm->next) {
say join('-', @number_set);
}


This is what it outputs:

Quote
D:\test>perl-1.pl
01-02-03-04
01-02-04-03
01-04-02-03
04-01-02-03
01-03-02-04
01-03-04-02
01-04-03-02
04-01-03-02
03-01-02-04
03-01-04-02
03-04-01-02
04-03-01-02
02-01-03-04
02-01-04-03
02-04-01-03
04-02-01-03
02-03-01-04
02-03-04-01
02-04-03-01
04-02-03-01
03-02-01-04
03-02-04-01
03-04-02-01
04-03-02-01



01-02-03-04
01-02-04-03
01-03-02-04
01-03-04-02
01-04-02-03
01-04-03-02
02-01-03-04
02-01-04-03
02-03-01-04
02-03-04-01
02-04-01-03
02-04-03-01
03-01-02-04
03-01-04-02
03-02-01-04
03-02-04-01
03-04-01-02
03-04-02-01
04-01-02-03
04-01-03-02
04-02-01-03
04-02-03-01
04-03-01-02
04-03-02-01



(This post was edited by FishMonger on Dec 16, 2012, 8:32 AM)


FishMonger
Veteran / Moderator

Dec 16, 2012, 8:43 AM

Post #13 of 15 (2930 views)
Re: [omega] cant output through perl or shell [In reply to] Can't Post


Quote
i was trying to write a quick and dirty script to print all possible combinations of 1-53 for a 6 slot entry


I missed the last part of that requirement. I was using the full 53 slots. With a little tweaking, or possibly using a similar module, we could adjust it to use only 6 slots.


FishMonger
Veteran / Moderator

Dec 16, 2012, 9:02 AM

Post #14 of 15 (2927 views)
Re: [FishMonger] cant output through perl or shell [In reply to] Can't Post


In Reply To

Quote
i was trying to write a quick and dirty script to print all possible combinations of 1-53 for a 6 slot entry


I missed the last part of that requirement. I was using the full 53 slots. With a little tweaking, or possibly using a similar module, we could adjust it to use only 6 slots.



Code
#!/usr/bin/perl 

use v5.10.1;
use strict;
use warnings;
use Math::Combinatorics;

my @n = '01'..'53';

my $combinat = Math::Combinatorics->new(count => 6,
data => [@n],
);

while(my @combo = $combinat->next_combination){
say join('-', @combo);
}



omega
Novice

Dec 16, 2012, 10:55 AM

Post #15 of 15 (2918 views)
Re: [FishMonger] cant output through perl or shell [In reply to] Can't Post

lol that certainly is a lot simpler/easier/manageable, thank you

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives