Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Totally stuck

Reply
Thread Tools

Totally stuck

 
 
David Arnold
Guest
Posts: n/a
 
      06-29-2004
All,

I have a file named "new".

\backtomargin


In Exercises~\ref{exer2.9.1}--\ref{exer2.9.2}, if the given
differential equation is autonomous, identify the equilibrium
solution(s). Use a numerical solver to sketch the direction field
and superimpose the plot of the equilibrium solution(s) on the
direction field. Classify each equilibrium point as either
unstable or asymptotically stable.






\ex\label{exer2.9.1} $P'=0.05P-1000$

The spacing is intentional. Now, my perl file is saved as new.pl.

use strict;
use warnings;

# replace \backtomargin with instructions environment
my @instructions;
my $instruct_line;
while (my $line=<>) {
chomp($line);
if ($line=~/^\s*\\backtomargin/) {
push (@instructions,$');
while ($instruct_line=<>) {
chomp($instruct_line);
if ($instruct_line=~/\\ex/) {
last;
} else {
push(@instructions,$instruct_line);
}
}
print "\\begin{instructions}\n";
# remove blank lines from end of array
while($line=pop(@instructions)) {
if ($line=~/^\s*$/) {
print "true\n";;
} else {
print "false\n";
push (@instructions,$line);
last;
}
}
# remove blank lines from the beginning of the array
while($line=shift(@instructions)) {
if ($line=~/^\s+$/) {
;
} else {
unshift (@instructions,$line);
last;
}
}
print join("\n",@instructions);
# empty instructions array for next pass
@instructions=();

print "\\end{instructions}\n\n";
print "$instruct_line\n";

} else {
print "$line\n";
}
}

When I enter:

perl new.pl < new

I get the following output:

D:\NewBook\conversion>perl new.pl < new
\begin{instructions}

In Exercises~\ref{exer2.9.1}--\ref{exer2.9.2}, if the given
differential equation is autonomous, identify the equilibrium
solution(s). Use a numerical solver to sketch the direction field
and superimpose the plot of the equilibrium solution(s) on the
direction field. Classify each equilibrium point as either
unstable or asymptotically stable.




\end{instructions}

\ex\label{exer2.9.1} $P'=0.05P-1000$


I am using ActiveState Perl on Win95. You might note the debugging
print statements and the fact that neither "true" nor "false" shows up
in the output.

I am completely stuck and in need of kind pair of eyes. If you can
help, it would be appreciated.

Thanks.
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      06-29-2004
"David Arnold" <> wrote in message
news: om...

>
> The spacing is intentional. Now, my perl file is saved as new.pl.



> # remove blank lines from end of array
> while($line=pop(@instructions))


> if ($line=~/^\s*$/) {
> print "true\n";;
> } else {
> print "false\n";
> push (@instructions,$line);
> last;
> }
> }



> I am using ActiveState Perl on Win95. You might note the debugging
> print statements and the fact that neither "true" nor "false" shows up
> in the output.


clearly the while() block is never executed, because
$line evaluates as false the first time through ('')

maybe you want something like
pop @instructions while $instructions[-1] =~ /^\s*$/;

gnari





 
Reply With Quote
 
 
 
 
Andrew Lee
Guest
Posts: n/a
 
      06-29-2004
On 29 Jun 2004 00:32:36 -0700, (David Arnold) wrote:

>All,
>
>I have a file named "new".
>
>\backtomargin
>
>
>In Exercises~\ref{exer2.9.1}--\ref{exer2.9.2}, if the given
>differential equation is autonomous, identify the equilibrium
>solution(s). Use a numerical solver to sketch the direction field
>and superimpose the plot of the equilibrium solution(s) on the
>direction field. Classify each equilibrium point as either
>unstable or asymptotically stable.
>
>
>
>
>
>
>\ex\label{exer2.9.1} $P'=0.05P-1000$
>
>The spacing is intentional. Now, my perl file is saved as new.pl.
>
>use strict;
>use warnings;
>
># replace \backtomargin with instructions environment
>my @instructions;
>my $instruct_line;
>while (my $line=<>) {
> chomp($line);
> if ($line=~/^\s*\\backtomargin/) {


But there is no space before "\backtomargin" ... so the following lines never
get executed.

> push (@instructions,$');
> while ($instruct_line=<>) {
> chomp($instruct_line);
> if ($instruct_line=~/\\ex/) {
> last;
> } else {
> push(@instructions,$instruct_line);
> }
> }



..snip

From what I can gather at a quick look, you are trying to nab everything between
"\backtomarin" and "\endofinstructions". Am I correct?

If so, try this :

if ($line=~/^\s+\\backtomargin/) {

and put whitespace before the delimeter "\backtomargin".

Better still, use a character or string that you don't expect to find elswhere
in the testfile, such as #.

Than you can say :

if ($line=~/^\#\\backtomargin/) {

.... etc.

HTH

 
Reply With Quote
 
thundergnat
Guest
Posts: n/a
 
      06-29-2004
Andrew Lee wrote:
> On 29 Jun 2004 00:32:36 -0700, (David Arnold) wrote:
>
>
>>All,
>>
>>I have a file named "new".
>>
>>\backtomargin
>>
>>
>>In Exercises~\ref{exer2.9.1}--\ref{exer2.9.2}, if the given
>>differential equation is autonomous, identify the equilibrium
>>solution(s). Use a numerical solver to sketch the direction field
>>and superimpose the plot of the equilibrium solution(s) on the
>>direction field. Classify each equilibrium point as either
>>unstable or asymptotically stable.
>>
>>
>>
>>
>>
>>
>>\ex\label{exer2.9.1} $P'=0.05P-1000$
>>
>>The spacing is intentional. Now, my perl file is saved as new.pl.
>>
>>use strict;
>>use warnings;
>>
>># replace \backtomargin with instructions environment
>>my @instructions;
>>my $instruct_line;
>>while (my $line=<>) {
>> chomp($line);
>> if ($line=~/^\s*\\backtomargin/) {

>
>
> But there is no space before "\backtomargin" ... so the following lines never
> get executed.
>


I don't know... It looks to me like there is zero or more spaces before
'\backtomargin'.



Actually, I think the logic problem is the lines:

> while($line=pop(@instructions)) {


and
> while($line=shift(@instructions)) {



Since the first line that gets popped off of the array is a blank line,
the while() short circuits and the code block never gets executed.


Seems to me you are doing an awful lot of extra work chomping off
newlines only to add them back in, and making arrays when you really
want strings. If I was trying to do something similar, I would probably
do something like:


use strict;
use warnings;

my $instructions;
my $instruct_line;

while (<>) {
if ($_ =~ s/^\s*\\backtomargin//) {
$instructions = $_;
while ($instruct_line=<>) {
last if ($instruct_line =~ /\\ex/);
$instructions .= $instruct_line;
}
$instructions =~ s/ *\n/\n/g;
while ($instructions =~ s/\n\n\n/\n\n/) {};
$instructions =~ s/^\n+//;
$instructions =~ s/\n{2,}$/\n/;
print
"\\begin{instructions}\n$instructions\\end{instruc tions}\n\n$instruct_line\n";
} else {
print "$_\n";
}
}



Not knowing what your exact formatting needs were, I took a guess based
on what it /looked/ what you were trying to do.
 
Reply With Quote
 
David Arnold
Guest
Posts: n/a
 
      06-29-2004
gnari,

> clearly the while() block is never executed, because
> $line evaluates as false the first time through ('')
>
> maybe you want something like
> pop @instructions while $instructions[-1] =~ /^\s*$/;



Thank you very much. Your suggestion pointed to the difficulty. I am
now using the following and all is working well.

use strict;
use warnings;

# replace \backtomargin with instructions environment
my @instructions;
my $instruct_line;
while (my $line=<>) {
chomp($line);
if ($line=~/^\s*\\backtomargin/) {
push (@instructions,$');
while ($instruct_line=<>) {
chomp($instruct_line);
if ($instruct_line=~/\\ex/) {
last;
} else {
push(@instructions,$instruct_line);
}
}
print "\\begin{instructions}\n";
# remove blank lines from end of array
pop @instructions while $instructions[-1]=~/^\s*$/;
# remove blank lines from the beginning of the array
shift @instructions while $instructions[0]=~/^\s*$/;
print join("\n",@instructions);
# empty instructions array for next pass
@instructions=();
print "\n\\end{instructions}\n\n";
print "$instruct_line\n";

} else {
print "$line\n";
}
}
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      06-29-2004
On 29 Jun 2004 00:32:36 -0700, (David Arnold)
wrote:

>I have a file named "new".
>
>\backtomargin
>
>
>In Exercises~\ref{exer2.9.1}--\ref{exer2.9.2}, if the given


Huh?!? Is this clpmisc or ctt?

>The spacing is intentional. Now, my perl file is saved as new.pl.


Ah, OK, then you want to (pre-)process a text (specifically, LaTeX)
file...

>use strict;
>use warnings;

[snip]

I was under the impression that your program was overly complex for
the task it was aimed at. OTOH I was too lazy to try to understand
*exactly* what you meant to do, so from your other post I grabbed the
"working version" and I tried it on your sample. Now, if I'm not
mistaken, the following should serve your needs and is considerably
simpler:

#!/usr/bin/perl -ln

print, next unless /\\backtomargin/;
{
local $/='';
chomp(my $par=<>);
print <<EOT;
\\begin{instructions}
$par
\\end{instructions}

EOT
}

__END__


>When I enter:
>
>perl new.pl < new


BTW: no need for '<',

perl new.pl new

will work!


HTH,
Michele
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"
 
Reply With Quote
 
Trent Curry
Guest
Posts: n/a
 
      07-01-2004
thundergnat wrote:
> Andrew Lee wrote:
>> On 29 Jun 2004 00:32:36 -0700, (David Arnold)
>> wrote:


[..]

>>> # replace \backtomargin with instructions environment
>>> my @instructions;
>>> my $instruct_line;
>>> while (my $line=<>) {
>>> chomp($line);
>>> if ($line=~/^\s*\\backtomargin/) {

>>
>>
>> But there is no space before "\backtomargin" ... so the following
>> lines never get executed.
>>

>
> I don't know... It looks to me like there is zero or more spaces
> before '\backtomargin'.


Correct, ^\s* optionally allows for any whitespace padding before
'\backtomargin'. The person you were quoting, Andrew Lee, was mistaken
To that person, I at least recommand O'Reilly's infinately useful
"Perl Pocket Reference, easily avaiable at any good book or computer
store.

> Actually, I think the logic problem is the lines:
>
> > while($line=pop(@instructions)) {

>
> and
> > while($line=shift(@instructions)) {

>
>
> Since the first line that gets popped off of the array is a blank
> line, the while() short circuits and the code block never gets
> executed.


Seems to me both of those while's should begin like this: while(defined
$line=...)
if nothing can be pop'ed ot shift'ed off anymroe, undef will be returned
instead, and THEN the loop condition will fail, which sems to be what
was desired, and not if the line was empty, but if something was
returned (aka testing if there is anything left in the array.)

--
Trent Curry -

perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1
)!eg;print(reverse("$s")."\n");'


 
Reply With Quote
 
187
Guest
Posts: n/a
 
      07-01-2004
Michele Dondi wrote:
> On 29 Jun 2004 00:32:36 -0700, (David Arnold)
> wrote:


> I was under the impression that your program was overly complex for
> the task it was aimed at. OTOH I was too lazy to try to understand
> *exactly* what you meant to do, so from your other post I grabbed the
> "working version" and I tried it on your sample. Now, if I'm not
> mistaken, the following should serve your needs and is considerably
> simpler:
>
> #!/usr/bin/perl -ln
>
> print, next unless /\\backtomargin/;


I dont udnerstand whats going on here. I thought 'next' can only be used
in loops, but here you're tryign to print something? Very confusing,
could you please explain what exactly is happening here?

> {
> local $/='';
> chomp(my $par=<>);
> print <<EOT;


And I don't see how you've opened the file. I thought <> read from a
file passed though STDIN, but you say below "no need for '<'" but
withotu that you're just passing "new" to new.pl, and not the file
contends, or am I missing something here..? Thanks for any explainations
you can give, aas I'm quite confused

> \\begin{instructions}
> $par
> \\end{instructions}
>
> EOT
> }
>
> __END__
>
>
>> When I enter:
>>
>> perl new.pl < new

>
> BTW: no need for '<',
>
> perl new.pl new
>
> will work!



 
Reply With Quote
 
Sam Holden
Guest
Posts: n/a
 
      07-01-2004
On Wed, 30 Jun 2004 22:31:53 -0700, wrote:
> Michele Dondi wrote:
>> On 29 Jun 2004 00:32:36 -0700, (David Arnold)
>> wrote:

>
>> I was under the impression that your program was overly complex for
>> the task it was aimed at. OTOH I was too lazy to try to understand
>> *exactly* what you meant to do, so from your other post I grabbed the
>> "working version" and I tried it on your sample. Now, if I'm not
>> mistaken, the following should serve your needs and is considerably
>> simpler:
>>
>> #!/usr/bin/perl -ln
>>
>> print, next unless /\\backtomargin/;

>
> I dont udnerstand whats going on here. I thought 'next' can only be used
> in loops, but here you're tryign to print something? Very confusing,
> could you please explain what exactly is happening here?


perldoc perlrun

And see the description of what the -n command line option that
is being passed to perl (via the -ln in the #! line) does.

>
>> {
>> local $/='';
>> chomp(my $par=<>);
>> print <<EOT;

>
> And I don't see how you've opened the file. I thought <> read from a
> file passed though STDIN, but you say below "no need for '<'" but
> withotu that you're just passing "new" to new.pl, and not the file
> contends, or am I missing something here..? Thanks for any explainations
> you can give, aas I'm quite confused


perldoc perlvar

And see the description of ARGV.

<> is equivalent to <ARGV> which manages the opening of files specified
on the command line.

--
Sam Holden
 
Reply With Quote
 
187
Guest
Posts: n/a
 
      07-01-2004
Sam Holden wrote:
> On Wed, 30 Jun 2004 22:31:53 -0700, wrote:
>> Michele Dondi wrote:
>>> On 29 Jun 2004 00:32:36 -0700, (David Arnold)
>>> wrote:

>>
>>> I was under the impression that your program was overly complex for
>>> the task it was aimed at. OTOH I was too lazy to try to understand
>>> *exactly* what you meant to do, so from your other post I grabbed
>>> the "working version" and I tried it on your sample. Now, if I'm not
>>> mistaken, the following should serve your needs and is considerably
>>> simpler:
>>>
>>> #!/usr/bin/perl -ln
>>>
>>> print, next unless /\\backtomargin/;

>>
>> I dont udnerstand whats going on here. I thought 'next' can only be
>> used in loops, but here you're tryign to print something? Very
>> confusing, could you please explain what exactly is happening here?

>
> perldoc perlrun
>
> And see the description of what the -n command line option that
> is being passed to perl (via the -ln in the #! line) does.


This still deosn't explain why there is a print statement there. Seems
useles there. Can someone please clearly explain this.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
it the Cisco switchprobe totally out of use? sqrfolkdnc Cisco 0 09-29-2005 03:05 AM
OSPF Totally stubby area Guan Foo Wah Cisco 1 08-04-2005 01:42 AM
Memcpy problem! Plz help me, I'm totally stuck here :( Paul Schouten C++ 2 01-03-2005 02:57 PM
Totally Unprepared =?Utf-8?B?S2V2aW4=?= MCSD 1 05-18-2004 04:10 PM
Failed 70-305 totally mismanaged time dunfailed MCSD 7 11-19-2003 10:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57