Processing Time [closed] - perl

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.

Related

How can i remove special character from a variable using perl [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 2 years ago.
Improve this question
This is my sample code
#!/usr/bin/perl
$file = SUN;
$file1 = abc.$file.cde
print "Value is : $file1\n"
I want Output like this abcSUNcde
What regular expression need for this.
As well ikegami said in his comment you are missing the semicolons at the end of the line (terminating the line or line should be ended).
#!/usr/bin/perl
$file = SUN;
$file1 = "abc${file}cde";
print "Value is : $file1\n";
This is will print abcSUNcde as an output of your code.
In case if you want to remove the special characters (what I understand except keyboarding characters)
$file2=~s/[^A-Za-z]//g;
Let try this and modify as well you can.

In perl, how to make a variable holding the number zero print as "0" instead of "" [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 7 years ago.
Improve this question
I've been wondering about this one for years... In perl, the number 0 when stored in a variable is wronging translated into a string as "" when printed. How to fix this problem?
For Example:
$X = 0;
print "X = $x\n";
One would expect this would print:
X = 0
instead it prints:
X =
Is there an easy way to make this print correctly without writing junky code like this:
$x = 0;
$xstr = !$x ? "0" : $x;
print "X = $Xstr\n";
This is the one thing i really hate about perl when trying to write programs that perform math and print math results..
Perhaps there is some "use" package that i can add to my code to fix the printing of number zero as a string?
You sure you got the case right? In the above you define $X but print $x instead. Perl is case sensitive.
$ perl -e 'print "X = $undefined\n"'
X =

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

Write to standard output [closed]

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.

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);