Re: Perl regular expression for implementing infix expression parser?

2016-08-14 Thread Richard Heintze via beginners
Well, I'm thinking of a simple syntax tree that would fit on a one-liner? For 
the first cut, I think I'll ignore unary operators and just accommodate numeric 
integer literals for addition, subtraction, multiplication and division.
Later we can add variables, unary operators, functions like log10, sin, cos 
etc...

See Recursive descent parser - Wikipedia, the free encyclopedia
e for expressionf for factorpm for plus or minusmd for multiply divide
n for numeric literal



here is my attempt so far (that does not work):
.! perl -pe 'BEGIN{ $n=qr{\d+}x; $pm=qr{[-\+]}x; $md=qr{[/\*]}x; 
$e=qr{$t($pm$t)?}x; $f=qr{$n|\($e\)}x; $t=qr{$f($md$f)?}}  s/($t)=( 
*)/"$1=".eval($1)/ge'
Here is my sample data55-23=
It seems to  ignore the 55.ThanksSiegfried 

On Sunday, August 14, 2016 4:26 AM, David Mertens 
 wrote:
 

 Hello siegried,

This is a fun question! In fact, v5.10 introduced "recursive subpatterns" into 
the regex lexicon, so using v5.10 or later, it is possible to implement (rather 
arcane) recursive descent regexes without resorting to eval-based tricks like 
what you have.

But before diving in, a few questions are in order to make sure we're working 
on the right problem. Are you looking for something to cram into a one-liner? 
(Do you have a character limit for your one-liner?) Are you open to solving 
this with a combination of for-loops and regexes, or do you want a purely 
regex-based approach? Are you trying to write something that merely validates 
that an expression as valid mathematics, or do you want to build an abstract 
syntax tree? Are you interested in using modules from CPAN or rolling your own? 
Finally, if your answer to all of these is, "I'm just curious!", then you can 
expect to get a very wide range of answers, not just regex-based.

Thanks! Looking forward to the fun!
David

On Sat, Aug 13, 2016 at 3:45 PM, Richard Heintze via beginners 
 wrote:

I this perl one liner for evaluating infix expressions in VI and emacs/VI 
emulator mode:


.! perl -MPOSIX   -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np }) )* 
\) }x;$funpat = qr/$np/;} s/($funpat)=($funpat|(")[^\"]* 
(")|[0-9\.]+)/"$1=$3".eval($1) ."$4"/ge'
That $np is from the camel book and it is a regular expression that parses 
nested sets of parentheses and then my replace command evaluates the arithmetic 
expression.
Since perl accommodates recursive regular expressions, it ought to be possible 
to implement a recursive decent parser.  
Can someone help me enhance the above code so that instead of just blindly 
looking for balanced parenthesis, the regular expression will recognize (match) 
this:
5+8*(2+8/(3+2))*(2+22/3)=()Thankssiegfried





-- 
 "Debugging is twice as hard as writing the code in the first place.
   Therefore, if you write the code as cleverly as possible, you are,
   by definition, not smart enough to debug it." -- Brian Kernighan


  

Perl regular expression for implementing infix expression parser?

2016-08-13 Thread Richard Heintze via beginners
I this perl one liner for evaluating infix expressions in VI and emacs/VI 
emulator mode:


.! perl -MPOSIX   -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np }) )* 
\) }x;$funpat = qr/$np/;} 
s/($funpat)=($funpat|(")[^\"]*(")|[0-9\.]+)/"$1=$3".eval($1)."$4"/ge'
That $np is from the camel book and it is a regular expression that parses 
nested sets of parentheses and then my replace command evaluates the arithmetic 
expression.
Since perl accommodates recursive regular expressions, it ought to be possible 
to implement a recursive decent parser.  
Can someone help me enhance the above code so that instead of just blindly 
looking for balanced parenthesis, the regular expression will recognize (match) 
this:
5+8*(2+8/(3+2))*(2+22/3)=()Thankssiegfried



Wanted: Help performing Date-Time Arithmetic using emacs/vi and perl

2016-04-18 Thread Richard Heintze via beginners
  I just love using viper mode in emacs to execute perl:
.! perl -MPOSIX -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np }) )* \) 
}x;$funpat = qr/$np/;} s/($funpat)=(.*)$/"$1=$3".eval($1)."$4"/ge'

This command searches for a balanced set of parens (from the camel book) 
followed by an equal sign followed by any trailing garbage and evaluates the 
expression inside the balanced parans.
so if you type
(4*atan(1))=gobblygook
and then execute the above VI/Ex command you get:
4*atan(1))=3.14159265358979


Here is my failed attempt to do date arithmetic:
(use Date::Calc ( ":all" ); use Date::Manip; my ( $date, $yy, $dd, $mm ); $date 
= scalar localtime( ( time() - ( 24 * 60 * 60 ) ) ); $date)=""
Can someone help me figure out how to use the eval function in my perl 
one-liner to evaluate date time arithmetic?
Thanks,Siegfried



How to write a function that clears an array?

2004-04-10 Thread Richard Heintze
I'm passing a singularly dimensioned array to a
function. I need to clear the array in the function so
that when I return to the main program, there are no
entries in the array variable I passed to the
function. 

Can someone write a small function that does this?
Thanks,
  Sieg

__
Do you Yahoo!?
Yahoo! Tax Center - File online by April 15th
http://taxes.yahoo.com/filing.html

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Perl Documentation like Javadoc

2004-02-10 Thread Richard Heintze
Speaking of documentaiton, is there any structured
approach to documenting the get/post parameters for a
perl web page?

I have a main menu type of of page I have inherited.
The GUI designer is not a programmer seems to have a
strong preference buttons instead of links or drop
down menus.

Each push button represents another target page which
has its own set of get/post parameters which require
its own, sometimes conflicting, hidden HTML fields. 
The code is SO ugly because the previous
programmers had no naming conventions and did not
understand SQL.

Is there any structured approach to say:

These hidden variables are for these destinatino
pages. These are the get/post parameters for this
page, etc...?

and
 Here are the get/post params this page needs?

Javadoc won't even help me with this (if I was using
Java).

   Siegfried

__
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
http://taxes.yahoo.com/filing.html

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




How to interrogate array cell?

2004-02-10 Thread Richard Heintze
I have an array of hashes. What function should I be
using to interrogate each array cell when I want to
know if it is occupied?

  "exists" seemed to do the job nicely. What about
"defined"?

Now I am curious: how would I implement a switch
statement (er, I mean, set of if-elsif statements) for
a hetrogeneous array where some array cells contain
arrays, others integers, other hashes?

I tried saying
  my @x;
 if(@x && $x[$ii] && %{$x[$ii]} && exists
$x[$ii]{"xyz"}){
 my $z = $x[$ii]{"xyz"};
   ...}

But that did not work. There must be some function
that will tell me "this array cell contains a hash,
this other array cell contains another array...".

  Thanks,
 Sieg

__
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
http://taxes.yahoo.com/filing.html

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Surpressing concatenation with null warnings

2003-11-04 Thread Richard Heintze
> I think what you want is this:
> 
> no warnings qw(uninitialized);
> 

Would I put this immediately after "use warnings;"?

__
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Surpressing concatenation with null warnings

2003-11-04 Thread Richard Heintze
I have just discovered the the following code causes
trouble when I have "use strict" and "use warn";

use strict;
use warnings;

my $k = $q->param('xyz');
print qq[ \$k = $k ];

The problem is that if there is no GET/POST param
called xyz, we are concatenating with a null value
and, when using CGI with Apache HTTPD, this is not a
problem (except it tends to make for large error
logs).

However, when using it with IIS it is a big problem
because the warning text gets inserted into the HTML
output (which is a BIG problem if you are in the
middle of emitting javascript code).

Is there a way I can suppress only concatenation
warnings? I did perldoc strict and perldoc warnings
and could not determine if the supression I want is
possible.

   Thanks,
  Sieg

__
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: $x .= $y suddenly breaks and does $x=$y instead!

2003-11-04 Thread Richard Heintze
Jenda,
  Sorry -- I was not quoting my own code precisely and
I am using strict and warnings.
  I am using parenthesis. I attached the exact code
for the subroutine below.


--- Jenda Krynicky <[EMAIL PROTECTED]> wrote:
> From: Richard Heintze <[EMAIL PROTECTED]>
> > After several hours I tracked it down to these
> line of
> > code. The concantenation is failing suddenly!
> > 
> > my $hidden="";
> > &FormElements(\$hidden...);
> > 
> > sub FormElements{
> >   my $hidden = @_;
> 
> This line is incorrect.
> 
> >   my $t1 = qq[];
> >   $$hidden .= $t1;
> 
> As
>   use strict; 
> would have told you.
> 
> The problem is that the
>   my $hidden = @_;
> sets $hidden to the number of elements in @_. Not to
> the first 
> parameter passed to FormElements().
> 
> So the $$hidden doesn't access the global $hidden
> variable, but a 
> variable whose name is the number.
> 
> Try to print the $hidden inside the subroutine!
> 
> You want either
>   my ($hidden) = @_;
> or
>   my $hidden = shift(@_);
> 
> 
sub FormElements {
  my ($me,
  $next, 
  $ev_count,
  $curr_true, # integer array created in insert
data
  $prob_innoc,
  $prob_guilt,
  $hidden) = @_;
  # The first time we view the Enter Probabilities
page (display) 
  # we have the option of specifing admissibility (the
probability of the evidence being admissible in court)
  # and the probability the fact is true.
  #
  # When there are subsequent assertions or suspects,
there is no need to allow the user to change this
information
  # so we surpress the text edit box.
  #
  my $bReadOnly = !$me->bFirstSuspect(); #
$me->{pass}==0 && $next==1; # Can the user change
this? Yes, if this is the first Suspect/Assertion.
  my $t3 = $me->{admissibility};
  my $admis = sprintf "%3d", $t3->[$ev_count];
  print 
($me->bShowAdmissibility()?(qq[
  ]
.($bReadOnly?qq[$admis ]:qq[])
.""):"");
  print qq[
  ];
  # older code: $me->{base_type}=~"Rank" ||
$me->{base_type}=~"RA"  || $me->{base_type}=~"Compare
Assertions"
  # old code: ($me->{base_type} ==
&case_constants::btCompareAssertions ||
$me->{base_type} == &case_constants::btRankSuspects)
  if ($me->MultiSuspectCase() && $bReadOnly){
# No input box here!
my $t2 = @{$$curr_true}[$ev_count]; # This is so
wierd. Why do I need to explicitly cast it?
$t2 = sprintf "%3.3f", ($t2<=0?0:$t2);
my $t3 = qq[];
print $t2.$t3;
# Why does not this concatenation work? 
$$hidden = $$hidden.qq[\nprob_true$ev_count$t3%\n];
  }
  else {
# Input box: user can alter this
print qq[ % ];
  }
  
  print qq|
  
  {pass}!=1){
print qq|
VALUE="|.($prob_guilt?$prob_guilt:"").qq|" |;
  }
  print qq| MAXLENGTH="6">%
  
{pass}!=1){
print
qq|VALUE="|.($prob_innoc?$prob_innoc:"").qq|"|;
  }
  print " MAXLENGTH=6>%\n";
}


__
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



$x .= $y suddenly breaks and does $x=$y instead!

2003-11-04 Thread Richard Heintze
I'm running perl 5.6.1 on Apache HTTPD V2.

I noticed some values were not appearing on the web
page.

After several hours I tracked it down to these line of
code. The concantenation is failing suddenly!

my $hidden="";
&FormElements(\$hidden...);

sub FormElements{
  my $hidden = @_;
  my $t1 = qq[];
  $$hidden .= $t1;
}

When single stepping thru this code with the
OpenPerlIDE debugger I see the new value of $$hidden
is just "" AFTER THE
CONCATENATION! It just did a copy instead of a
concatenation! This explains where my table went when
I look at the web page!

I tried rewriting the problem line of code:
  $$hidden = $$hidden.$t1;

This did not help -- same results!

I tried 
   $$hidden = qq[$$hidden$t1];

Same results! No concatenation again!
Am I loosing my mind?
Sieg

__
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Where is editor that will Indent my perl code?

2003-11-03 Thread Richard Heintze
Tim (or anyone else)
I have vim 6.1 and tried it out. I know VI (a little)
so I thought vim would not be so bad. I tried help and
searching help for indent and found it. It looks more
like a function call for their macro language than it
does a command. I tried ":indent(6)" and it did not
work.

 How do I use VIM to re-indent a perl function?

  Also, I assume by your response that PerlBuilder
indents code too?

   Thanks,
  Siegfried

--- Tim Johnson <[EMAIL PROTECTED]> wrote:
> 
> PerlBuilder's syntax highlighting works very well,
> but it's also more
> expensive than many of the other alternatives.  Have
> you tried vim?
> 
> -----Original Message-
> From: Richard Heintze
> [mailto:[EMAIL PROTECTED] 
> Sent: Monday, November 03, 2003 11:28 AM
> To: [EMAIL PROTECTED]
> Subject: Where is editor that will Indent my perl
> code?
> 
> 
> emacs and a number of other editors have the
> ability,
> with a single key stroke to properly indent java
> code.
> 
> However, since perl has such unusual syntax for
> specifying literal character strings (my favorite is
> qq[]) emacs chokes when it attempts to indent my
> perl
> code. Too bad -- I love emacs.
> 
> Is there an editor out there that will properly
> indent
> my perl code even if I use the stranger syntaxes for
> literal strings?
> 
>   Thanks,
> Siegfried
> 
> __
> Do you Yahoo!?
> Exclusive Video Premiere - Britney Spears
> http://launch.yahoo.com/promos/britneyspears/
> 
> -- 
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
> 
> 
> --
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
> 


__
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Where is editor that will Indent my perl code?

2003-11-03 Thread Richard Heintze
emacs and a number of other editors have the ability,
with a single key stroke to properly indent java code.

However, since perl has such unusual syntax for
specifying literal character strings (my favorite is
qq[]) emacs chokes when it attempts to indent my perl
code. Too bad -- I love emacs.

Is there an editor out there that will properly indent
my perl code even if I use the stranger syntaxes for
literal strings?

  Thanks,
Siegfried

__
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



How to store arrays in hashes or objects?

2003-10-28 Thread Richard Heintze
I had emailed this query out previously but since I
never saw my own email in the digest, I'm assuming
that it never made it to the [EMAIL PROTECTED] list.
Please forgive me if it did and I did not see it (my
SPAM filter might have eaten it).


Question #1
---
I have an array stored in an object and I trying to
compute the length of the array. This seemed to work
initially:

my $nColumns = [EMAIL PROTECTED]>{component_titles}}}+1;

Something changed, I don't know what, and perl started
dying on the above statement with no error message or
explanation. I had to resort to this technique:

my @ComponentTitles = @{$me->{component_titles}};
my $nColumns = $#ComponentTitles+1 ; 

This seems to work, but I don't like it because it
uses a supefluous deep copy. Can someone tell me how
to do it in one statement without a superfluous deep
copy?

Question #2
---

Incidently, I believe I'm storing an array here
instead of a reference to an array.

I don't really do anything to create to create this
array -- I just start storing elements like this:

  $me->{component_titles}[0] = "xyz";

Is there a better way to populate this array? Perhaps
with a declaration or something?

Question #3
---

I tried to create a second array and store it by
reference like this:

my $a = [];
$me->{a} = \$a;
$a->[0] = 1;
$a->[1] = 23;
etc...

But when I tried to retrieve the array, there was
nothing there:

print $me->{a}->[0];

I cured the problem by storing elements directly, but
I did not want to make a new copy of the array (I
wanted to store a reference to the original array).

  $ii = 0;
  $me->{a}->[$ii++] = $_ foreach (@{$a});


Why does not this work:
 $me->{a} = \$a;

  Thanks,
 Siegfried

__
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



SQL/Win32::ODBC/MSAccess from Perl 5.6?

2003-08-01 Thread Richard Heintze
Is this an appropriate place to post questions on
Win32::ODBC/MSAccess 2000/Perl 5.6 cgi/Apache HTTPD? 

If not, can someone directe me to an appropriate FAQ
or mailinglist or newsgroup?

I've been struggling with specifying zero length
strings in SQL. Nothing seems to work.

I've tried 
$Data->Sql(qq[UPDATE Suspect SET Name = "" WHERE
CaseNumber = $sCaseNumber AND  Number = 33]);

and 

$Data->Sql(qq[UPDATE Suspect SET Name = '' WHERE
CaseNumber = $sCaseNumber AND  Number = 33]);

These statements both give me syntax errors from the
ODBC driver.

I've also tried using the prepare and bind_param
functions apparently these are not implemented -- they
are not in the documentation.

 Thanks,
 Siegfried

__
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Why did @{$$a}[$i]; fix my problem?

2003-06-19 Thread Richard Heintze
 The following fragment of code retrieves an integer
 from an array that is passed by reference. It was
 working fine:
 
 my $t = @$curr_true[$ev_count];
 
 I made some changes else where in the program (from
 which this fragment comes) and suddely both the web
 server and (thank goodness) the debugger started
 aborting on the above statement! I had not changed
 this portion of the program!
 
 So as suggested by earlier advice I had received
 from
 this mailing list, changed it:
 
 my $t = $$curr_true[$ev_count];
 
 That did not help! This did not help either:
 
 my $t = $curr_true}->[$ev_count];
 
 Finally, in dispare, I started randomly
 experimenting
 and found that this fixes the problem (at 3AM):
 
 my $t = @{$$curr_true}[$ev_count];
 
 Why would this fix the problem? I have plenty of
 other
 array references that use the $$x{$i} syntax and
 they
 still work fine.
 
 I'm using perl 5.6.1 on NT4 SP6.
 
Thanks much,
  Siegfried


__
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Sudden termination for no reason?

2003-06-17 Thread Richard Heintze
My program is terminating for no reason apparent to
me!
Using the debugger I see that $$curr_true is a 4
element array whose values are , 1, 4, 7.

$ev_count is 1.

Why would both my debugging session suddenly and my
cgi script end when I execute that last statement?
  Thanks,
 Siegfried

sub FormElements {
my ($g_Case,
$next, 
$ev_count,
$curr_true, # integer array created in insert
data
$prob_innoc,
$hidden) = @_;
  my $t = $$curr_true[$ev_count];

   Sieg

__
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



%x = ( xyx => 'abc' ) is strange!

2003-06-17 Thread Richard Heintze
Why does this program print "yes def" but not "yes
xyz"? It does print "xyz:def", so I don't understand
why it does not print "yes xyz".

{
  my %x = ( xyx => 'abc', d => 'y', f => 'g' );
  $x{"def"} = "fhi";
  print  qq($_ : ).$x{$_}.qq(\n)   foreach (keys %x);
  foreach (keys %x) { print "yes xyz\n" if ($_ =~
"xyz");  }
  foreach (keys %x) { print "yes def\n" if ($_ =~
"def");  }
  print $x{'xyz'}."\n";
}

__
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



How to store bit array of arbitrary length

2003-06-15 Thread Richard Heintze
Presently I'm using an integer to implemement an array
of booleans.

I suspect this won't work beyond 32 array elements. Is
there a better way to accommodate longer bit arrays?

Could I use a string, for example, to store an array
of bits? Can I just use the bit manipulation operators
(^= &= |=) on a string?

How would I store such a bit array in an access database?

__
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Wanted: Help with modules/packages

2003-06-12 Thread Richard Heintze
I apologize if I'm posting this twice. I assume that
it did not go out to the group because I did not get
any responses and the folks at [EMAIL PROTECTED] have
been extremely helpful in the past.

Can somone help me make my sample program work below?
(1) I cannot call function hello from main.pl. Why?
(2) In main, is it necessary to say $test::x? Is there
a way to say $x instead of $test::x?
(3) Why does not "starting..." ever appear?
  thanks,
  Siegfried

I have two files:
test.pm---
package test;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($x hello);
my ($x) = ("testx");
sub hello { print "hello\n"; }
BEGIN { $Exporter::Verbose=1; print "starting...\n"; }
1;

main.pl---
use strict;
use warnings;
use test qw($x hello);
$test::x = "there";
print "\$test::x = $test::x\n";
&test::hello();



__
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Wanted: Help with Function Arguments

2003-06-01 Thread Richard Heintze
James,
The problem occurs when $q->param('next') returns a
null of sorts because there is no such parameter in
the URL. Apparently, this does not get passed.

When this is the case, all the subsequent function
parameters are offset by one.

  Very strange...
   What a language...
   I'll try your suggestion of using scalar a
little later...
 Siegfried


--- James Edward Gray II <[EMAIL PROTECTED]>
wrote:
> On Saturday, May 31, 2003, at 05:01  PM, Richard
> Heintze wrote:
> 
> > Thanks James, I think I understand now!
> 
> Glad to hear it.
> 
> > Here is another topic:
> 
> My, my you are quite the problem child today, aren't
> you?  ;)
> 
> > What is happening to my second argument here? It
> is
> > not being passed!
> >
> > &print_args("abc", $q->param('next'), "xyz");
> >
> > # However, if make a slight change, it works!
> > my $n = $q->param("next");
> > &print_args("abc", $n, "xyz");
> >
> > What is going on here? That was nasty!
> 
> Hmm, the only difference I can see between these two
> calls is the 
> context.  The first call is in list context, because
> of the subroutine 
> call, but the second is scalar context as it is an
> assignment to a 
> scalar variable.  Does it work, if you change the
> first call to:
> 
> print_args("abc", scalar $q->param('next'), "xyz");
> 
> ???  If that works, it's a context issue.
> 
> Consulting 'perldoc CGI' we can see that param()
> will return an array 
> in list context for multivalued parameters.  If that
> happened I'm 
> guessing you had more entries than you thought in
> that subroutine call 
> and probably didn't see 'xyz' print out, unless it
> returned an empty 
> list which I can't think of any reason it would do
> if you know there's 
> a value in there for the scalar call.
> 
> That's my best guess there.  If that doesn't help,
> you need someone who 
> knows more about CGI than me.  :D
> 
> Good luck.
> 
> James
> 
> P.S.  You don't need those '&' on the sub calls and
> eventually, they're 
> going to surprise you.  Go ahead and drop them.
> 
> P.P.S.  Your variable names aren't improving, don't
> make me beat you 
> with the I-Will-Name-My-Variables-Well-Stick!  :)
> 


__
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Wanted: Help with Function Arguments

2003-06-01 Thread Richard Heintze
Thanks James, I think I understand now!

Here is another topic:

What is happening to my second argument here? It is
not being passed!

use CGI qw(:standard);
$q = new CGI;
sub print_args {
  my ($a, $b, $c) = @_;
  print "a = $a b = $b c=$c\n";
}
&print_args("abc", $q->param('next'), "xyz");

# However, if make a slight change, it works!
my $n = $q->param("next");
&print_args("abc", $n, "xyz");

What is going on here? That was nasty!

  Thanks,
 Siegfried


__
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Wanted: Help with perl 5.6 syntax Arrays, Hashes and references

2003-06-01 Thread Richard Heintze
James,
 I hope this is my last question. I appreciate your
(and everyone else's that has contributed)
generousity.

I have a web site I inherited where a single page has
3000 lines of perl code. It does not use strict or
warnings. The original authors only used global
variables and never used any function arguments.

So I needed to pass arrays as function arguments (to
keep my sanity) so here is what I did (see below). It
seems to work. Since I am passing an array by
reference, I am using @$ to dereference the function
parameter. That makes sense to me.

Did I do it wrong? (see below). I'm beginning to think
so.

Why does "@$y[$i]" work here, and "foreach $i (keys
%$x)" works to fetch the keys from a reference to a
hash, but I use $$x{$i} to fetch hash reference
element values instead of %$x{$i}?

This seems very inconsistent to me.

sub print_array {
  my ($y, $n) = @_;
  for my $i ( 0 .. $#$y ) {
print "print_array using .. y[$i] = @$y[$i]\n";
  }
}
# It was not my idea to use undeclared variables! 
# Are we creating a reference to a hash here?
$undeclared[0] = 'a';
$undeclared[2] = 'b';
$undeclared[3] = 'c';
&print_array([EMAIL PROTECTED], 3);

  Thanks,
 Siegfried

__
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Wanted: Help with perl 5.6 syntax Arrays, Hashes and references

2003-06-01 Thread Richard Heintze
Sorry James, you got a second copy. I meant to reply
to the group.

James,
thank you, thank you!

Can you explain this syntax then, that is used with
foreach loops? 

foreach my $i (keys %{$x}) { ... }


Why don't we use foreach my $i (keys $$x){... }? 

> > What is the name for this syntax: "(keys %{$x})"?

Thanks,
  siegfried

__
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Wanted: Help with perl 5.6 syntax Arrays, Hashes and references

2003-06-01 Thread Richard Heintze
Thank you very much James and David! Wow! What prompt
responses!

I have some more questions!

I tried "use strict;" and that worked. Are you
encouraging me to use "use warn;" too? That does not
work.

> > # $i receives the proper values
> > foreach my $i (keys %{$x}) {
> >   # (4) Why does not this work? How do I index
> into my
> > hash?
> >   print "hash i = $i => ".$x{$i}."\n";
> 
> Just like you did the array in the other print call
> above, ${$y}{$i}.  

What you say works! 

What is the name for this syntax: "(keys %{$x})"? Are
we dereferencing and casting? Why do I use "%" here
but when I want to access a specific element, you say
to use the syntax "${$y}{$i}". The index operator {}
needs to work on the entire hash, not a a scalar! By
using a $, we indexing into a scalar, no?


> >
> > my %z= ('d' => 'y', 'f' => 'g');
> > foreach my $i (keys %z) {
> >
> > # (5) Why does $z work instead of %z here?
> >print "z{$i} = $z{$i}\n";
> 
> Because we're talking about a single scalar value
> now, not the whole 
> hash.

Same question again! Why are we indexing into a
scalar? 

  Thanks!
  Sieg
> > }
> 
> Hope this helps.
> 
> James
> 
> 
> -- 
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
> 


__
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Wanted: Help with perl 5.6 syntax Arrays, Hashes and references

2003-06-01 Thread Richard Heintze
Some help understanding this program would be greatly
appreciated! I'm really struggling with this perl
language!
  Thanks,
 siegfried

my $x= {'d' => 'y', 'f' => 'g'},
$y = ['a', 'b', 'c', 'd'];
# This works! Good!
foreach my $i (@{$y}){ print "array i = $i\n" }

# (1) Why does not this work? How do I index an array?
# (2) How do I compute the length of y instead of hard
coding 3?
for(my $i = 0; $i <= 3; $i++) { 
  print "y[$i] = "[EMAIL PROTECTED]"\n"; 
}

# $i receives the proper values
foreach my $i (keys %{$x}) {
  # (4) Why does not this work? How do I index into my
hash?
  print "hash i = $i => ".$x{$i}."\n";
}


my %z= ('d' => 'y', 'f' => 'g');
foreach my $i (keys %z) {

# (5) Why does $z work instead of %z here?
   print "z{$i} = $z{$i}\n";
}

__
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]