LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2009, 03:04 AM   #1
sagik
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Rep: Reputation: 0
Question (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 ?



thanks !

sagi.
 
Old 02-28-2009, 06:05 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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;
 
Old 02-28-2009, 07:16 AM   #3
sagik
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
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'!

Code:
0000000:  0807 0308 0701 0807 0008 0100 2800 0fff ..........{...
0000010:  0807 0308 0701 0807 0038 0100 2804 00ff ..............
0000020:  0807 0308 0701 0807 0208 0100 2800 0f1f ..............
 
Old 02-28-2009, 07:57 AM   #4
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 510

Rep: Reputation: 53
Use a regex if you want the hex stuff treated as something string-like or use pack/unpack.

Und read this for all the details on slurping "stuff" from files into "something" in Perl.

http://www.perl.com/pub/a/2003/11/21/slurp.html

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.
 
Old 02-28-2009, 08:12 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
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.
You can extract bytes or other chunks of a scalar containing binary data by using the unpack() function.

perldoc -f unpack

--- rod.
 
Old 02-28-2009, 10:42 AM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by sagik View Post
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?
 
Old 02-28-2009, 11:39 AM   #7
sagik
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
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)

Kapish ?
 
Old 02-28-2009, 12:16 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 03-01-2009, 05:20 AM   #9
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 510

Rep: Reputation: 53
Quote:
Originally Posted by sagik View Post
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,
You know about the USB-module...?

http://search.cpan.org/~gwadej/Device-USB-0.28/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script having multiple grep statements-I want input file to be read only once mukta9003 Linux - Newbie 4 08-27-2008 12:58 AM
[Perl] cgi.pm - save input in .html file noir911 Programming 2 01-07-2007 02:36 PM
Read a char from a file (PERL) linuxlover1 Programming 4 01-09-2005 09:10 AM
read the input file and write in the given format suchi_s Programming 8 12-17-2004 01:12 AM
read the input file from the specified line no till end suchi_s Programming 5 09-09-2004 04:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration