Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Can anyone suggest to me how to read a video file from Perl without using any third-party tools?
I know the opencv library for Python and C. I am not sure which one to use for Perl.
UPDATE
I get the output as
3 bytes read
´ˇÙ
Argument "M-+M-^?M-t" isn't numeric in bitwise and (&) at
0
I am novice in perl and I am missing something. I am reading 3 bytes from the file till EOF. I want to mask it and do some manipulation on the bits. I am reading pack/unpack it really doesn't make a clue to me.
open (FILE, "<:raw", $InputFile) or die "Couldn't open";
my ($buf, $data, $n);
while (($n = read FILE, $data, 3) != 0) {
print "$n bytes read\n";
$buf = $data;
print $buf . "\n";
my $maskNumber = 0x4;
my $value = ($buf & $maskNumber);
print $value . "\n";
}
Perl's bit operators have string modes and numeric modes; if either parameter is a number, the numeric mode is used.
So I suspect you want something like:
$buf & "\0\0\4"
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 2 years ago.
Improve this question
Wondering if it's possible to write a short script that prints the code itself out in the end with the lines counted. Even something simple like a few lines of
#!/usr/bin/perl
use strict;
print "Hello there";
print "This got me scratching my head";
And the output would be this code itself with the lines counted.
Thanks a lot in advance.
Something like this?
use warnings;
use strict;
print "Hello there";
print "This got me scratching my head";
open (my $f, '<', $0);
while (<$f>){print};
print "read $. lines\n";
The variable $0 or $PROGRAM_NAME holds the name of your program.
Or (with linecount on each line)
use warnings;
use strict;
print "Hello there";
print "This got me scratching my head\n";
open (my $f, '<', $0);
while (<$f>){printf "%03d %s",$., $_};
print "read $. lines\n";
The variable $. or $INPUT_LINE_NUMBER contains the current line number for the last filehandle accessed.
See perlvar
Also see mobs answer for a way to read the file using DATA
The name of the script being executed is in the variable $0, so the straightforward way to accomplish this is
...
open(my $ZERO,"<",$0);
my #lines = <$ZERO>;
close $ZERO;
print #lines, "count = ", 0+#lines, "\n";
...
When $0 is unavailable because you have changed directories or overwritten it, another option is to use the special DATA handle which is opened on a file that contains the special __END__ or __DATA__ tokens.
...
seek DATA, 0, 0; # seek to begin of file, not begin of __DATA__ section
my #lines = <DATA>;
print #lines, "count = ",0+#lines,"\n";
...
__DATA__
...
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 9 years ago.
Improve this question
Can anyone tell me why I'm getting this:
usage: gen-non-random.pl <count> <outputfile>
From the code below:
#!/usr/bin/perl -w
#
# Script to generate non random values, to demonstrate a bad randomness graph
# for my "Howto Analyse SessionIDs".
#
# written by:
$version = "0.0.4";
$filename = "gen-non-random.pl";
$usage = "usage: $filename <count> <outputfile>\n";
$count = $ARGV[0] or die ("$usage\n");
$output = $ARGV[1] or die ("$usage\n");
print ("-- $filename Version: $version\n");
use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
use Math::Random;
use Digest::MD5 qw(md5_hex);
open (OUT, ">$output") or die ("Can't open $output\n");
for ($i=0; $i<$count;$i++)
{
# generate a random number
$random = random_uniform();
# cut out char 3-9 of $random and put it in $randsub
$randsub = substr($random, 2, 6);
# get seconds and microseconds since epoch
($seconds, $microseconds) = gettimeofday;
# get the last two chars of the seconds and put them into $s
$s = substr($seconds, 8, 2);
# sleep for a while
usleep $randsub;
# put together the last two digits of seconds and the microseconds
$time = $s . $microseconds;
$md5_time=md5_hex($time);
# print out the stuff we put together above
print OUT ("$md5_time\n");
}
close (OUT) or die ("Can't close $output\n");
print ("$count values written to $output\n");
exit;
I am new to programming so i need really simple answer please! I do not own this code I am using for my research paper at University. Also, could someone please explain to me what Usage actually is i can't seem to find a good explanation for it?
Thanks.
You're getting that error because you're not using the program correctly:
usage: gen-non-random.pl <count> <outputfile>
This basically means you have to provide a count and output file as arguments, such as:
perl gen-non-random.pl 42 outfile.txt
This will generate forty-two numbers and output them to the outfile.txt file.
It's the two lines near the start, checking ARGV[0/1] and die-ing if you don't provide them, that are outputting this message and exiting the program.
Hmmm. I can't run the above code because Time::HiRes::ualarm() is not implemented on Windows. That said, it appears to be generating a MD5 has string of the current time (in integer form) after sleeping for a random number of seconds, then dumping the result into a text file. You are getting the usage message mentioned above because the program expects input. Try running it from the command line like so:
perl gen-non-random.pl 10 MyResults.txt
I suspect that will dump 10 HD5 hash results into a file called "MyResults.txt".
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 9 years ago.
Improve this question
I am using CGI in strict mode and a bit confused with variables. I am reading a file that has two lines. Storing both in two variables. But when i try outputing them using html, it says global variable error
This is what I am doing
open TEXT, "filename";
$title = <TEXT>;
$about = <TEXT>;
close TEXT;
but this gives the global variable error. whats the best way to fix this?
You need to declare variable with my to make its scope local. This is the best practice and compulsory when using strict
use strict;
use warnings;
open my $fh, '<', 'filename' or die $!;
my ( $title, $about ) = <$fh>;
close $fh;
Further improvements:
Avoided bareword file handles (e.g. FILE). Instead use local file handles such as my $fh
Used error handling with die when dealing with file processing
Combined assignment of $title and $about as suggested by #Suic
use warnings to display what's going wrong as pointed out by #TLP
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 want to remove every lines in a text file that contain the word "script" and then write whatever left into another file. But keep the originl file.
like this:
open the file
delete any line with the word "script"
then output whatever left after delete to another file.
perl -ne '/script/ or print' file > newfile
grep -v script original.file > new.file
Or if you really need perl:
#!/usr/bin/perl
use strict;
use warnings;
open(my $in, '<', 'input.txt')
or die "Cannot open input.txt: $!";
open(my $out, '>', 'output.txt')
or die "Cannot open output.txt: $!";
while (<$in>) {
print $out $_ unless /script/;
}
close($in);
close($out);
Finally, if you are only looking to match "script" if it is a word (and not part of a bigger string like "prescription" or "scripting") then change:
/script/
To:
/\bscript\b/
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'm writing a Perl script. If the text in a specified column (here column 13) matches a certain text namely 'one' or 'two', then the whole line (so all columns) should be copied to another file. My input is a tab-delimited .txt file.
This is what I have so far:
my $table1 = $ARGV[0];
open(my $variants,$table1) || die "$! $table1";
open(my $out,'>',"filtered.txt") || die "Can't write new file: $!";
while(<$variants>){
chomp;
my #line=split(/\t/); #split on tabs
if (($line[12] =~ m/one/) || ($line[12] =~ m/two/)){
print $out "$_";
}
}
Since I'm getting a 'use of uninitialized value' error, I wanted to know what needs to be changed in this code.
What is the problem?
perl -F'\t' -ane'print if $F[11]=~/one|two/' input > output
This is a great example of a program that should be written using the Unix filter model.
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
my #line = split /\t/;
# Do you mean 12? That's the 13th field
print if $line[12] =~ /one/ || $line[12] =~ /two/;
}
Simpler to write and easier to understand. Oh, and far more flexible (no hardcoded filenames).
Call it like this:
$ ./my_filter < input_file.txt > output_file.txt