Write to standard output [closed] - perl

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I have a perl process which run every day automaticaly, it takes about 20 minutes and finnish and print every opertion to stdout worht to mention every operation takes about second .
Sometimes it seems that the stdout is stucked and i need to click enter in order for the process to continue running .
could someone experience such issue/and suggestion how to handle this issue ?
below a snapshot of my code:
foreach (<>) {
chomp;
if (validate($_) == 0) {
print "$_ validated with Success\n";
}
else {
print "$_ validated with failure\n";
}
}

As Bill Ruppert writes, you need to use while (<>) instead of foreach (<>) in order to allow the print statements to be executed after each line of input is read.
As Joop Eggen writes, you need to make sure your standard output is not written in blocks in order to make sure that each line of output appears immediately after a print statement has been issued. This can be done by setting the $| variable.

Related

A Perl User Input program can't be executed in Ubuntu [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm a beginner of Perl, using Ubuntu 12.04 as my operating system.
My perl version is v5.14.2.
I wrote a simple program to read user input then print it.
#!/usr/bin/perl
print "please enter your name:";
my $name = ;
print "\n";
print "hello, $name\n";
When I tried to execute this program it turned out to be like this,
syntax error at input.pl line 3, near "=;"
Execution of input.pl aborted due to compilation errors`
But this is a sample code which I copied from a perl textbook, so it shouldn't be any mistake in the code.
that code makes no sense, as the compiler tells you:
my $name = ;
write something like:
my $name = <>;
To be more explicit:
my $name = <STDIN>;
which reads from the standar input, or:
my $name = readline(*STDIN);
If you do my $name = <>;, it will read either from a file specified on the command line or from STDIN if no file is specified

Processing Time [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Why does the following script take so many cycles to complete?
The document it is sifting through is 20590 lines long and each line consists of the following, or a variation thereof;
"10160354001 ALGIERS ALGERIA 36.70 3.60 290"
I am trying to make a database to match the first number to the last 3.
The actual program is however taking several seconds per line.
Script:
#! /usr/bin/perl
$path = shift(#ARGV);
open COORDS, $path or die "Couldn't open file\n";
my %coords = {};
foreach $line (<COORDS>){
$stat = substr $line,0,11;
#coords = grep(($_!=undef),split(/ /,substr $line,42));
$coords[$stat]=#coords;
}
print $coords['10160354001'];
close(COORDS);
$coords['10160354001'] = ... is an assignment to an array element, and a large one at that. This statement will cause Perl to allocate an array with room for at least 10160354002 elements.
You meant to use a hash:
$coords{$stat} = "#coords";
...
print $coords{'10160354001'};
use strict and use warnings would have alerted you to this and other problems with your code.

Difference between Sprintf and printf in Perl [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the difference between sprintf and printf in Perl?
I'm really confused with those functions.
I know about printf. It is used for STDOUT, but I want know in depth of these functions.
sprintf just returns a formatted string, printf prints it to a filehandle.
printf HANDLE "%s", $arg
can (very redundantly) be written as
$formatted = sprintf "%s", $arg
print HANDLE $formatted
Of course, this specific example is most naturally written as
print HANDLE $arg
because the format string I used for an example is so trivial as to be useless.
Of course, HANDLE is optional, and defaults to STDOUT, although you can also change the default with select.

Randomizing 3 lines to display in CGI with Perl [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a CGI script that will take three lines of text and randomize them. Each time you view the webpage, the three lines will appear one after the other in a different order each time. How do I do this and what is the code?
perldoc -q "random line"
Found in D:\sb\perl\lib\perlfaq5.pod
How do I select a random line from a file?
Short of loading the file into a database or pre-indexing the lines in
the file, there are a couple of things that you can do.
Here's a reservoir-sampling algorithm from the Camel Book:
srand;
rand($.) < 1 && ($line = $_) while <>;
This has a significant advantage in space over reading the whole file
in. You can find a proof of this method in *The Art of Computer
Programming*, Volume 2, Section 3.4.2, by Donald E. Knuth.
You can use the File::Random module which provides a function for that
algorithm:
use File::Random qw/random_line/;
my $line = random_line($filename);
Another way is to use the Tie::File module, which treats the entire file
as an array. Simply access a random array element.
or
perldoc -q shuffle
Found in D:\sb\perl\lib\perlfaq4.pod
How do I shuffle an array randomly?
If you either have Perl 5.8.0 or later installed, or if you have
Scalar-List-Utils 1.03 or later installed, you can say:
use List::Util 'shuffle';
#shuffled = shuffle(#list);
If not, you can use a Fisher-Yates shuffle.
sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
return unless #$deck; # must not be empty!
my $i = #$deck;
while (--$i) {
my $j = int rand ($i+1);
#$deck[$i,$j] = #$deck[$j,$i];
}
}
use List::Util qw( shuffle );
#lines = shuffle(#lines);

$ARGV[0] is not working in Solaris SPARC [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I used $var=$ARGV[0] in some Perl code on a Solaris x64 machine and it is receiving the argument correctly.
But the same piece of code is not working in Solaris SPARC.
Any clue?
Also $_[0] is working in Solaris SPARC, but then it is not working in Solaris x64.
Is there any other way?
Try this program:
use strict;
use warnings;
print join ": ", #ARGV . "\n";
Run it with a bunch of command line arguments, and tell me what you're getting as an output. It should look something like this:
$ myprog.pl one two three four five
one: two: three: four: five
Next, try the same thing with this program:
use strict;
use warnings;
print join ": ", #ARGV . "\n";
my $value = $ARGV[0];
print qq(My value = "$value"\n);
Now, edit your question to show us the output you're getting. This way, we'll know what you mean. Also, give us at least a code snippet of what is not working, what you expect, and what you're getting.
Writing a quick etest program is always a good way to track down an issue, and can give you something to post on Stackoverflow if you're still stuck.