Ask a Question related to PERL Beginners, Design and Development.
-
William M West #1
skipping lines of input from another program...
the following is my script that i am working on- it fails to do
quite
the right thing.. qouting from one of the comments below::
"#skip_lines() is supposed to "skip" some lines of output.
#The problem is, that it DOESN'T even though the debug print statement
# "print "trash is $trash \n"; #debug " shows that it works-
#after leaving the for loop, it seems that CMD re-winds itself...
# i am flummuxed!!"
#!/usr/bin/perl
#
#
# Screen parser for Service Information System utilities
# on RAXP
# History::
# 5 feb 2004 skeleton and parse routines built (w/ test values.) wmw
#---------
use strict;
use diagnostics;
#----------
my $command = "/home/willy/Documents/conditional.testbed.exp";
my $pattern = "inittab";
my $skip;
$skip->[0] = 2;
$skip->[1] = 2;
my $table;
$table->{"PART"} = "INVENTORY";
#----------
# $pattern is only a string right now because i am assuming
# that the screens will be consistant with each call. $skip should produce
# enough flexibility for reuse.
open (CMD , "$command|") or die "Could not execute $command : $!";
parse_and_extract($pattern,$skip,$table);
close CMD;
#the following routine takes each line of the input (from a different
#program). When the pattern is matched, it calls get_inventory().
sub parse_and_extract {
my $pattern = shift; #what we look for.
my $skips = shift; #array ref. each value is # of lines to
skip
# over.
my $table = shift; #values are stored here. (hash reference)
while (<CMD>) {
get_inventory ($table,$skips) if ($_=~m/$pattern/);
print "parse_and_extract\n\r"; #debug
print "$_\n\r"; #debug
}
}
#the following routine assigns a key/value pair to a hash reference ($table)
#skip_lines() is supposed to "skip" some lines of output.
#The problem is, that it DOESN'T even though the debug print statement
# "print "trash is $trash \n"; #debug " shows that it works-
#after leaving the for loop, it seems that CMD re-winds itself...
# i am flummuxed!!
sub get_inventory {
my $table = shift;
my $skip = shift;
#$skip is an array reference for the case that several
#different screens will have to be parsed in the future-
#in which case, a flag will have to be added to determine
#which value in the array reference will be used.
# (perhaps $skip should be a hash of arrays?)
#first we skip the lines from the pattern match up to
#the line with the value we want.
skip_lines ($skip->[0]);
my $partnumber = $_;#check syntax-> is $_ instead?
#skip lines to the next value...
skip_lines ($skip->[1]);
my $inventory = $_;#check syntax-> is $_ instead?
$table->{$partnumber}=$inventory;
#print "keys:: "; #debug
#print keys(%$table); print "keys to the table\n"; #debug
#print " get_inventory() $_\n"; #debug
}
sub skip_lines {
my $lines = shift; #number of lines in file to skip over
print "in skip_lines() $_ before skip\n"; #debug
#<CMD> for 1..$lines; #not functioning...
for (0..$lines){
my $trash = <CMD>;
print "trash is $trash \n"; #debug
}
print "in skip_lines() $_ after skip\n"; #debug
}
#---------------------------------------
the following is the test expect script that i am using- as far
as i can tell, it's working fine :/ (there's extra stuff in there
since i was trying to learn how to use "if" statements earlier...)
#!/usr/bin/expect -f
#set ps $argv 0
set ps "password"
spawn telnet "localhost"
expect "ogin: "
send "willy\n"
expect "word:"
send "$ps\n"
expect "terminal:*"
send "ls\n"
#------------------
set file "Desktop.ini"
set counter 1
while {$counter < 5} {
send "echo $counter \n ls $file\n"
expect "No such file" {set counter [expr $counter + 1]}
expect "$file" {set counter 200}
sleep 2
}
send "ls\n\n"
sleep 2;
expect "terminal" {
if {$counter < 200} {
global subject
set subject "failed to produce report"
#expect "terminal:*"
send "w\n"
}
if {$counter > 100} {
#expect "terminal:*"
send "date\n"
}
}
expect "terminal"
send "cd /etc\n"
expect "terminal"
send "ls\n"
#------------------------------
interact
thanks,
willy
William M West Guest
-
MP3 Skipping
Hi, I have an MP3 streaming application that has been running for almost a year now. I have been hearing a lot of skipping like a scratched record... -
Blank Lines in program code
After editing the Home page in Contribute 2 many blank lines are inserted into the page source code. The size of the page keeps increasing... -
How to handle input and output of a program executed by System function in perl
"Jürgen Exner" <jurgenex@hotmail.com> wrote in message news:<JmBKc.4704$Iz3.2470@nwrddc01.gnilink.net>... Hi, I think Perl is good at dealing... -
Newbie - skipping lines of a file.
tadmc@augustmail.com (Tad McClellan) writes: The irony is that I somehow failed to recognise this as a FAQ! I just answered the question... -
How to program order entry with input dependent filters
Hi folks. Thanks in advance for you help. We're in the midst of a wholesale revision of our Access database to SQL so that orders can be directly... -
William M West #2
RE: skipping lines of input from another program...
in the post that i am now replying to, i said that i couldn't get
the program to skip lines as it parsed through the output of another
program.
i have "solved" the problem- but i do not know WHY this works the
way it does.
<SNIP>
this worked great :) (thanks to drieux) - in fact, it would let
me have several programs input to this one at once :) (if i'm not mistaken)
>open (CMD , "$command|") or die "Could not execute $command : $!";
>
>parse_and_extract($pattern,$skip,$table);
>
>close CMD;
<SNIPPED for brevety>
>
>sub skip_lines {
> my $lines = shift; #number of lines in file to skip over
>
> print "in skip_lines() $_ before skip\n"; #debug
>
> #<CMD> for 1..$lines; #not functioning...
>
> for (0..$lines){
> my $trash = <CMD>;
> print "trash is $trash \n"; #debug
> }
the fix was to replace the above for loop with the following::
for my $trash (0..$lines) {
$_ = <CMD>;
print "the line skipped is $_\n"; #debug
}
> print "in skip_lines() $_ after skip\n"; #debug
>
>}
well- i don't understand it. WHY does this work?
if someone could clarify the mechanics behind this behaviour,
i would be very grateful!
thank you much,
willy
William M West Guest
-
Rob Dixon #3
Re: skipping lines of input from another program...
William M West wrote:
Get just one working, for now :)>
> in the post that i am now replying to, i said that i couldn't get
> the program to skip lines as it parsed through the output of another
> program.
>
> i have "solved" the problem- but i do not know WHY this works the
> way it does.
>
> <SNIP>
>
>
>
> this worked great :) (thanks to drieux) - in fact, it would let
> me have several programs input to this one at once :) (if i'm not mistaken)
[snip]
Hi Willy.>> >
> >sub skip_lines {
> > my $lines = shift; #number of lines in file to skip over
> >
> > print "in skip_lines() $_ before skip\n"; #debug
> >
> > #<CMD> for 1..$lines; #not functioning...
> >
> > for (0..$lines){
> > my $trash = <CMD>;
> > print "trash is $trash \n"; #debug
> > }
>
> the fix was to replace the above for loop with the following::
>
> for my $trash (0..$lines) {
> $_ = <CMD>;
> print "the line skipped is $_\n"; #debug
> }
>>> > print "in skip_lines() $_ after skip\n"; #debug
> >
> >}
>
> well- i don't understand it. WHY does this work?
>
> if someone could clarify the mechanics behind this behaviour,
> i would be very grateful!
It's because $_ wasn't being assigned. Your original calls to
skip_lines read like this:
skip_lines ($skip->[0]);
my $partnumber = $_;
skip_lines ($skip->[1]);
my $inventory = $_;
but you were reading your record into $trash instead. It's a bad
idea to use $_ as a global variable. It's meant to be used invisibly
and in very close scope in things like:
while (<>) {
s/this/that/g;
print;
}
and not for passing stuff out of subroutine calls. If you're not sure
then just don't explicitly mention '$_' at all and you should be safe.
I think you have an over-elaborate design for this anyway. I think it's
likely that your data can be processed better than just by skipping 'n'
lines between useful records. And even if this is the way you want to go
there's nothing wrong with
<CMD> for 1 .. $skip->[0]
my $partnumber = <CMD>;
<CMD> for 1 .. $skip->[1]
my $inventory = <CMD>;
HTH,
Rob
Rob Dixon Guest
-
William M West #4
RE: skipping lines of input from another program...
well- my design is likely not that great over all *laugh*>
>I think you have an over-elaborate design for this anyway. I think it's
>likely that your data can be processed better than just by skipping 'n'
>lines between useful records. And even if this is the way you want to go
>there's nothing wrong with
but it works well enough :) i have taken your code below and
am implementing it-- i think it's clearer what's going on.
when i tried the "<CMD> for 1 .. $n;" idea, at first i wanted to use $_>
> <CMD> for 1 .. $skip->[0]
> my $partnumber = <CMD>;
>
> <CMD> for 1 .. $skip->[1]
> my $inventory = <CMD>;
>
>HTH,
>
>Rob
again... i finally understand that it's not being assigned any value at
this point- it still retains the last value from the while loop...
i wanted to take
while (<CMD>){
$x = $_;
}
and simply insert your line like so::
while (<CMD>){
<CMD> for 1 .. $borkborkborkbork;
$x = $_;
}
for me this is a more intuitive idiom, though one i
now know to be false.
while (<CMD>){
<CMD> for 1 .. $borkborkborkbork;
$x = <CMD>;
}
seems to be the proper (only?) method in this particular case.
:)
thank you,
willy
William M West Guest



Reply With Quote

