(perl)- trying to read from file that keep geting input in parallel- help!
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
(perl)- trying to read from file that keep geting input in parallel- help!
hey guys,
iv'e been trying for several hours to do a little programm :
Code:
$file = $ARGV[0];
open FILE, $file;
while (1)
{
@array=@array.<FILE>; #add the lines to array from the file.
print @array; #prints it.
}
explanetion :
i have a device that functions like a button , and i've found that if u read the /dev/input/event5 with xxd, i can see that the same values changes when i press the button or moving it clockwise...
my problem is - how to read from this file, and how to get that several values into var's ?
What you have up there isn't Perl code. It looks (sort of?) like Ruby or I-don't-know-what. Perl doesn't use the @array.<FILE> syntax.
Here's how to get items from a file into an array:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
my $file = $ARGV[0];
open my $file_handle, '<', $file
or die "Can't open $file for reading: $!";
my @lines;
while (<$file_handle>) {
push @lines, $_;
}
print @lines;
it's perl ... the "@array.<FILE>" thing is for the lines of the file will be added to the array (using the dot thing in the middle).
thanks anyway!,
one more question, the output that written into the file that i'm reading, is in binary... i red it using xxd. so, how can i split the HEX values and take just the values that i want? cause in this case is just like one line of string and not seperate values.
Ex. of output:
i want to get the bold values to var'!
With smaller files, I prefer the two birds with one stone method:
Code:
sub read_my_file {
my $filetoread = shift;
my $filehandle;
my @filelinebyline;
my $fileallatonce;
open($filehandle, "<$filetoread") or die "Can't read $filetoread: $!\n";
if(wantarray) {
@filelinebyline = <$filehandle>;
} else {
local $/; #slurp mode
$fileallatonce = <$filehandle>;
}
}
(No, I don't name my vars like this in real code.
If you call read_my_file in an array-context, Perl returns the @filelinebyline, if you need a scalar, you get $filealletonce.
it's perl ... the "@array.<FILE>" thing is for the lines of the file will be added to the array (using the dot thing in the middle).
I'm happy to admit that I don't know everything about Perl. It's a big language, and there are lots of ways to do things (to mangle a phrase).
Nevertheless, the code you posted originally makes very little sense. Here's what I see:
An infinite while loop with no way to break out of it
An assignment to an array using . which is - I thought - a binary operator to join strings. (I've never seen that used in an @array assignment, and have little idea what it should do in one.)
So, for fun, I rewrite your program to have a finite while loop, and I run it: here's what I have and the output I get:
Code:
telemachus ~/practice $ cat liner
#!/usr/bin/env perl
use strict;
use warnings;
my $file = $ARGV[0];
open FILE, $file;
my @array;
my $number = 0;
while ($number <= 10) {
@array = @array.<FILE>;
print @array;
$number++;
}
telemachus ~/practice $ ./liner parser
0#!/usr/bin/env perl
1use strict;
1use warnings;
1
1my %installed_with_deps;
1
1while (<>) {
1 if ( m/^(\S+)\s+has no/ ) {
1 $installed_with_deps{$1} = undef;
1 }
1 elsif ( m/^(\S+).*:$/ ) {
What's happening? Well, every time through the loop, you're assigning this to the array
Code:
@array . <FILE> # concatenate the number of items in @array and one line of FILE
Perl interprets the first bit (@array) as scalar @array which is why it's 0 the first time and 1 every time afterwards. Then, it takes <FILE> also in a scalar context, so it grabs one line from the file each time. The assignment thus gets 0 or 1 concatenated with one line of FILE and assigns that to array. Since you are assigning to the array each time (rather than using push or shift), the old contents of @array are overwritten each time.
So, sure, that's Perl in the sense that it compiles, but it's insane. What are you actually trying to do?
first thank you all!!
second,Telemachos, what i've been trying to do is very simple thing - i have a button connected to the computer via usb, it's input written in dev/input/event5.
now, all i want to do is ti create an "Driver" that controls the volume gane of my linux while i'm moving this button clockwise or backwards.
I've discovered that if i read this /dev/input/event5 , i'll see that just the 11th and 12th values in the hex have some changes while playing with the button.
so i want to write a script that allways read the file (and that's way the infinity loop - to keep reading the hex's canges)and with a switch case , check the value and then do it's thing ( in my case, raise up a var with limit of 50 and minimum of 0...consider it the sound volume gane)
You probably need to reset the file pointer back to the beginning of the file on each iteration, or close and reopen the file to re-read it on each iteration. The example code shown with 'perldoc -f seek' serves very nearly the same purpose as you require.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.