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.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I want to write a Perl subroutine using rand() function that generates a random DNA sequence of specified length n. The length n of the sequence is passed as an argument to the subroutine.
I would appreciate if someone could help me out as I am a beginner in perl.
FYI normally you put anything that you have tried so far, Stack Overflow isn't a code writing service.
With that in mind, the best way to do this in my humble opinion is with Perl's rand function:
#!/usr/bin/env perl
use strict; use warnings;
use autodie ':all';
use feature 'say';
my #letters = qw(A C G T);
sub random_DNA {
my $length = shift;
my $seq = '';
foreach my $n (1..$length) {
$seq .= $letters[rand(4)]
}
return $seq
}
foreach my $length (1..9) {
say random_DNA($length)
}
which outputs
con#V:~/Scripts$ perl random_DNA.pl
T
TT
TGG
TGTC
ATGAC
AACGAG
CGGGGTT
CCGTCGTC
TGGCCTCGA
your output will probably not be identical to this, of course, as this is a random function. I prefer not to use modules if I can avoid them, to avoid portability issues, especially with tasks that take a minute to write, like this one.
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.
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
Suppose I have a sentence/paragraph:
This cat is very cute.
Here, "cute" is the 5th word in the sentence. If I know the index of the first letter of this word - in this case c, 17 - how can I find out the position of this word in the sentence?
Counting the number of spaces in the substring and then adding 1 to it would probably work.
#!/usr/bin/perl
use strict;
use warnings;
my $in = "This cat is very cute.";
my $sub = substr $in, 0, 17;
my $word_count = scalar(split " ", $sub) + 1;
print "$word_count\n";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
input:
EMP_T4_SHARED_IRQ_o,198
EMP_T5_SHARED_IRQ_o,199
EMP_T6_SHARED_IRQ_o,237
oDRF_LPINT2G_v4_main,201
output:
EMP_T4_SHARED_IRQ_o,198
EMP_T5_SHARED_IRQ_o,199
oDRF_LPINT2G_v4_main,201
EMP_T6_SHARED_IRQ_o,237
sort the text file based on second field in perl..
perl -F',' -ane'
push #r,[ $_, $F[1] ];
END{ print map $_->[0], sort {$a->[1] <=> $b->[1]} #r }
' input
or
perl -e'
print
map $_->[0],
sort{ $a->[1] <=> $b->[1] }
map [$_, (split /,/)[1] ], <>;
' input
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 want 0 replacing '०' Unicode character. I am using a Perl script and trying the code given below.
my %table_digits =
(
'०' => '0'
'१' => '1', .....)
It's working fine with other Unicode characters. Those are being replaced by other numbers. But it is not able to replace '०' with 0. How it can be done?
See Unicode numeric value in Unicode::UCD:
use 5.010;
use utf8;
use open ':std', ':utf8';
use Unicode::UCD qw(num);
for my $digit (qw( ० १ २ ३ ४ ५ ६ ७ ८ ९ )) {
say "$digit==".num($digit);
}