Re: Sufficient effort

2004-12-02 Thread Tagore Smith
Trying out a new email client so I hope the attributions are right- if 
not, I apologize in advance. Been a while since  I posted to the list...

sentinel wrote:
finitely not however as I mentioned recently, simply ignoring those 
who aren't trying is the game plan.  After a couple of weak attempts 
they usually leave.  I rather liked Wiggins responses to those who 
don't try.  Point them to specific perldocs references.  It's more 
effort than they put in (maybe) but at least it get's them studying.
Also remember that what seems trivial to you may be incomprehensible to 
a new programmer for whom perl is a first language. If you've been 
programming for a few years, in one language or another, you will have 
developed a mental model of how code gets turned into instructions, and 
that is a very useful guide to what you should be doing- not everyone 
has developed that, and it may be that it takes them a lot of effort to 
formulate a badly formed question- that might be sufficient effort.

I programmed a lot in basic as a kid, but I was still totally stumped 
the first time I ran into a C compiler- well that was before widespread 
internet access, and I did eventually figure it out, but I shudder, 
thinking about how clueless my cries for help would have been had I had 
access to a list like this (but the effort I put in on that was well 
beyond sufficient- it was wastefully heroic- if you learned to program 
in the pre-web era from a couple of horribly written manuals, you'll 
understand what I mean). I remember how clueless I was trying to figure 
out Imake, etc (well, as you know there's only one Imake file in the 
world- just been modified a few times- but if it were ever lost it's 
unlikely anyone would be able to come up with a fresh one...)- I blamed 
compiler bugs for a lot of stuff for a while- as in, there must be a bug 
in this compiler :).  I also remember harassing Borland's technical 
support claiming there was an install problem when in fact I lacked the 
necessary mental model to figure out their build tool.

I'd also claim (perhaps incorrectly), based on later experience, that I 
have an above average aptitude for programming- as do a lot of people on 
this list, particularly the most knowledgeable. It's likely that at 
least some of the posters to the list will have an _even harder time_ 
with the basics than I, or you, did... which is saying something in my 
case at least.

Not that I'm above being rude to someone 'cause I think they are being 
lazy- but before you make that judgement you should establish that it is 
laziness rather than newbishness which is implicated. If you lack a 
mental model for programming you will ask some astonishingly thick 
questions, and respond to clear, concise answers with 'but I don't get 
it... and I have no real idea of how to get it'. I know I would have, 
had there been someone to ask. I still do, occasionally ;).

Hopefully he hasn't left but then again (this might sound harsh) I'd 
rather he get a tougher skin and deal with it.  Nothing personal mind 
you.  It's simply life.
P.S.  Gunner come back.  Don't run man :-)
Good deal if he feels like it- but maybe he's happier only dealing with 
people who know their way around.

Anyway, I know this thread is supposed to be dead, but since it refuses 
to die, I felt like putting in my two cents- sorry, admin, and everyone 
who wants it to go away.

Tagore Smith
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Re: find the lowest number?

2002-08-30 Thread Tagore Smith

Felix Geerinckx wrote:

> For small array sizes, it doesn't matter which method you use, since
> the cpu time will be near zero anyway. For larger sizes it does
> matter, and the loop method is a clear winner.
> Therefor, imho, your suggestion in March (and mine now :-) to use a
> loop, is *always* preferable.

*Always* is a very strong word :). For small array sizes the method may make
a difference if you're going to be performing the operation many times.
Sudarshan's banchmark called sort twice and reverse once when it only needed
to call sort once, and it was still much faster than the linear scan for an
array with seven elements. If you had an application that spent most of its
time finding the minimum and maximum element of small collections the sort
method would be preferable. It's also idiomatic (didn't Randall Schwartz
post about this once saying it was an example in the Llama book?) and imho
quite readable.

There are other situations in which you might prefer a log n to a linear
algorithm for this kind of thing. If you need to repeatedly find minimum and
maximum elements of a collection over a period in which insertions and
deletions are being performed you might want to keep the collection sorted.
Likewise if you collect the elements over time and want to amortize the cost
of the operation. If your data is too large to fit in memory a log n
algorithm may beat a linear algorithm badly for all reasonable values of n
if the linear algorithm uses a lot of disk seeks. Without knowing the
details of the application it's not possible to know what algorithm is best.

I suspect that a lot of the people who ask this question are dealing with
small arrays, in which case the sort solution makes sense. It's also kind of
cool :). But the caveat about large arrays _is_ important- don't want people
using it on an array with fifty-million elements! (Of course if you're
trying to maximize performance working with large datasets you might be best
off using C or Lisp anyway, or using an RDBMS to do your heavy lifting- and
hopefully you're not dealing with these kinds of datasets if you're at the
stage where you're first learning how to find the maximum element of an
array).

This (how do I find the greatest/lowest element?) comes up every few months.
Maybe there should be a list faq that covers this and other
questions that get asked a lot and aren't in the Perl faq :).

Tagore Smith



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




Re: Checking against an assosiative array...

2002-04-01 Thread Tagore Smith

Daniel Falkenberg wrote:


>%users = (
>  'username01' => {
>  'Comp01' => '$0.00'
>  },
>   'username02' => {
>  'Comp02' => '$0.00'
>},
>   'usename03' => {
>   'Comp03' => '-$9.90'
>  }
>);
>
>My questing is if I have the following variable...
>
>$variable = "usename01";
>
>How can I check that against the hash above.  If $variable doesn't match
>anything the print "Username does not match";

I'm not sure if you just want to check if the username exists, or get the
values associated with it. Also, it seems like you won't know the keys of
the nested hash. If that's the case the following checks to see if the key
$user exists and then gets the values in the associated hash.

if (defined(%{$users{$user}})){
while((my $key, my $value)=each(%{$users{$user}})){
print "$key: $value\n";
}
}
else {
print "No match for $user.";
}

Tagore Smith


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




Re: HTML whine

2002-04-02 Thread Tagore Smith

Jonathan E. Paton wrote:

> Closing HTML tags is part of the specification, i.e.
> not optional for syntactically correct HTML.
>
> Such HTML only works with most browsers due to the
> proliferation of badly coded HTML.  If it wasn't
> for that, then browsers wouldn't have such a hard
> time parsing HTML (and would therefore be faster).
>
> Alas, web designers cut back on HTML tags to improve
> the download times - a mixed blessing.

Not to nitpick (but I'm going to anyway :) ), but closing some tags (like p)
is optional in all versions of HTML based on SGML. SGML allows for end tag
minimization. Closing tags is mandatory in XHTML- xml does not allow for end
tag minimization (in fact even empty tags, like img and br, must be closed).
I don't know off the top of my head if the spec allows end tag minimization
for option elements, but I agree that it is good form to close the tags.
Since recent web servers and browsers increasingly use compression, and
repeated elements compress well, bandwidth is becoming less of an issue.

Tagore Smith


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




Re: Parsing formdata from one CGI script to the next.

2002-04-08 Thread Tagore Smith

Daniel Falkenberg wrote:


> Hello All,
>
> Is it possible to parse form data from one CGI script to another one.
> What I want to do is have a user login to a page.  From their I the user
> to select on a link then that link will take them to another CGI
> script.  In this new CGI script I need that to contain some information
> of the first script.

   There are two basic approaches to this. Either you store state on the
server or on the client. It is simplest to store state on the client by
having the first script generate a form that includes values from the first
form as hidden fields. If the information is sensitive you should be careful
about this- you could wind up caching the information on the client (you can
use a no-cache pragma, but there isn't a guarantee that all user-agents will
respect it) and if a user leaves a machine unattended someone could see the
information by viewing the source of the page.

   You can also store state in a cookie (which has a number of problems), or
store it in some fashion on the server, although this requires some sort of
session tracking (so the second script knows whose information to use).

   Btw, are you subscribed to beginners-cgi? It is specifically for cgi (in
Perl).

Tagore Smith


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




Re: complementing a scalar

2002-05-25 Thread Tagore Smith

mark wrote:

> Actually this goes back to my initial question about this:
>
> $name =~ tr/a-zA-Z0-9-_ .,:;'&$#@!*()?-//cd;
>
> What is happening here exactly and what precisely does the /c do in this
> statement?  I've had a couple of answers that I do not understand and thus
> far I've been told that /c "complements" the SearchPattern.  OK

> So my question is (drumroll) : Do I take it to mean that /c also adds the
> negative values of the given SearchPattern to the SearchPattern?
Otherwise,
> what is the "complement of /a-z/ if not /-(a-z)/ ?

You should take another look at John W, Krahn's answer to your question- it
is explained there. I'll try to put it in my own words, but I don't
guarantee it will be as accurate as John was. The complement referred to
here is set complement- that is, all items not in the set. The d means to
delete any characters for which you haven't given a translation (and in the
tr above you aren't giving any translations). So your tr means delete any
characters in $name that aren't in the set of characters given between the
first two / characters. I think :).

Tagore Smith



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




Tail call optimization

2002-05-28 Thread Tagore Smith

I came across this statement on the web:

>Perl ... supports the tail-call optimization (although you have to do it by
hand).

I was wondering if someone could give me an example of this. I know what
tail-call optimization means, and how to write tail-recursive functions, in
Lisp- just not sure how you would do this in Perl.

Thanks
Tagore Smith




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




Re: Is there a good Perl way for a validation subroutine?

2002-05-28 Thread Tagore Smith

Leila Lappin wrote:


> Hello all,
>
> I need to write a subroutine that validates several fields and returns one
summary field containing names of what >failed.  I was thinking of
concatenating names of all fields that failed and return that string from
the subroutine.  Then >check the string (return value) and if it's not null
update the data base with it.  Is that considered a good style for >Perl?

You're going to run into trouble just concatenating the field names- how
would you be able to tell where one name started and another left off? You
could separate them with some character and use split to get an array
containing the names- but if you're going to do that, why not just push them
onto an array and return that?

in pseudocode:

my @failed;
if (field fails validation){
   push @failed, $fieldname;
}

etc.

Strings hide the structure of your data- to use data (other than a single
string of course :) ) stored in a string you're going to have to go to the
extra trouble of parsing the string. Better to use a data structure that
supports the operations that you're going to need.

But if all you're going to do with the return value is use it in a sql
statement it would be reasonable to write a subroutine that returned a piece
of that sql statement as a string (which would be a little (but maybe not
much) more complicated than just concatenating the fieldnames), since that's
all you will need.

Tagore Smith


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




Re: Tail call optimization

2002-06-03 Thread Tagore Smith

Jenda Krynicky wrote:


> I believe they meant "goto &NAME".



> This way perl doesn't create a new record in the call stack every
> time you "call" the _fib().
> As you can see if you comment out the return in fib_() and remove
> the comment from "croak ..." and "use Carp;". (die() with stack
> print).
>
> See
> perldoc -f goto

Thanks :). That's great. I don't use goto much, except for a couple of very
specific situations, so I hadn't  read the docs for goto. It seems I missed
a very interesting beast in goto &NAME. In fact , I sent a friend of mine
some code recently that would have been improved in one place by its use.

But I still have a question :). As I understand it when tail-call
optimization is done automatically there are two advantages. One is that the
stack doesn't grow out of control, so that you don't have to worry about
blowing it up if you recurse very deeply. The other is that the overhead of
successive calls is eliminated. In theory (but, I think, often not in
practice) the optimized routine should be as efficient as the equivalent
iterative routine. Using goto &name has the first advantage, but does it
have the second? That is, does goto &NAME have the same overhead as a normal
call?

Does anyone write this kind of recursive function regularly? I mean, is it
idiomatic in the more rarefied Perl circles? Or is it better (from a style
point of view) to use iterative constructs even in places where recursion is
more natural, but tail-call optimization would be required?

Thanks
Tagore Smith


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




Re: finding max value

2002-02-13 Thread Tagore Smith


Jeremy Vinding wrote:
> duh... thx
> of course, the results still favor sort:
>
> Benchmark: timing 100 iterations of for 10_000 elems, for 20 elems,
sort
> 10_000 elems, sort 20 elems...
> for 10_000 elems:  7 wallclock secs ( 5.11 usr +  0.02 sys =  5.13 CPU) @
> 194931.77/s (n=100)
> for 20 elems:  8 wallclock secs ( 5.44 usr +  0.01 sys =  5.45 CPU) @
> 183486.24/s (n=100)
> sort 10_000 elems:  4 wallclock secs ( 2.99 usr + -0.02 sys =  2.97 CPU) @
> 336700.34/s (n=100)
> sort 20 elems:  3 wallclock secs ( 3.22 usr + -0.04 sys =  3.18 CPU) @
> 314465.41/s (n=100)

An O(n log n) algorithm doesn't grow _that_ much faster than one that's O(n)
(log n grows much less quickly than n). I'm not that experienced with Perl,
but I assume that the sort built-in is a fast n log n routine written in C
(quicksort?). The C implementation may be enough faster that it overwhelms
the difference in complexity for n < some large number (or there may still
be something weird with your benchmark). Of course an O(n) algorithm will
always beat an O(n log n) algorithm if you pick n large enough.

My experience programming in another interpreted language similar to Perl
(in architecture) has shown me that using built-ins can be dramatically
faster than crunching stuff yourself.


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




Re: help with first Perl application (repost w/ better subject line)

2002-03-03 Thread Tagore Smith
 PLAYED file is used to keep track of what cards have been
> #   played.  This file populates the playedCards select box.
>
> open (PLAYED,"played.txt");
> @playedCards = ;
> close PLAYED;
>
> if (playedCardsCheckbox){
>
>  print param("playedCardsCheckbox");
>  foreach my $item (param("playedCardsCheckbox")){
>  print @playedCards[$item],"";
>  push(@playedCards, "$Draw[$item]");
>  }
> }

Now you retrieve the values. But the indexing of @Draw has changed.

> open (PLAYED,">>played.txt");
> foreach my $item (@playedCards){
> print PLAYED "$item";
> }
> close PLAYED;

   This is wrong. Try this- Start a game, play one card, and then hit the
submit button a few times. You'll see that the list of played cards grows,
even though you're not playing any cards. This is because you keep appending
the played card file onto itself. You need to overwrite the played card
values, or just append the newly played cards.



   The main problem here is that the control flow is a bit confusing,
particularly with all that writing and reading of files, sometimes
repeatedly. You might want to rethink it some.

Tagore Smith


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




Re: multidimensional hashes

2002-03-03 Thread Tagore Smith

Chris Garaffa wrote:

> Hello all,
> I'm working on a site for a t-shirt company, and doing the order process
> in Perl.
> I've got everything working, and working fine, but I was wondering about
> different ways to go about doing this:
>
> $the_order = new CGI;
> $the_sleeve_type = $the_order->param('sleeve_type');
> $the_previous_step = $the_order->param('step');
> %product_to_number = ();
> %number_to_description = ();
> $product_to_number{"short"} = "21";
> $product_to_number{"long"} = "20";
> $number_to_description{"21"} = "short sleeved t-shirt";
> $number_to_description{"20"} = "long sleeved t-shirt";
>
> (The above is simplified... There are many entries in product_to_number
> and number_to_description, as well as many more params passed to
> $the_order.
> Now, how do I go about defining multidimensional arrays, and then
> accessing them? What I have now is this:
>
> $product_number = $product_to_number{$the_sleeve_type};
> $product_description = $number_to_description{$product_number};
> print "[$product_number] [$product_description] ";
>
> but I'd like to be able to get rid of %product_description altogether
> and make calls to a multidimensional hash
> Any ideas?
>

Well if you're hardcoding the values as above you can write:

#!/bin/perl -w
use strict;

my %products=();

$products{short}= {
   number   => 21,
   description  => 'short sleeved t-shirt',
};

$products{long}= {
   number   => 20,
   description  => 'long sleeved t-shirt',
};

print "$products{short}{number} $products{short}{description}\n";
print "$products{long}{number} $products{long}{description}\n";

If the values are coming from variables the syntax is essentially the same,
i.e.

my $short='short';
my $shortnum=21;
my $shortdescription='short sleeved t-shirt';

$products{$short}= {
   number       => $shortnum,
   description  => $shortdescription,
};

There are, of course, other ways to write this. What's going on is that each
value in %products is a reference to an anonymous hash. Perl doesn't really
have hashes of hashes- it has hashes of references to hashes.

Tagore Smith



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




Re: help with perl/Tk

2002-03-07 Thread Tagore Smith

richard noel fell wrote:
> Below is a simple script which I am using as part of my attempt to learn
> how to program a gui with perl and Tk. I have copied the program from a
> book, "Cross Platform Perl" (not very good, but some isolated good
> parts), but get the following error message when I try to compile the
> program. Has anyone any idea what is wrong?
> Thanks in advance,
> Dick Fell
>
> [rfell@rosewall webproject]$ tktext.pl
> Unquoted string "end" may clash with future reserved word at ./tktext.pl
> line 57.
> Can't modify negation (-) in scalar assignment at ./tktext.pl line 8,
> near ""File","
> Execution of ./tktext.pl aborted due to compilation errors.
> [rfell@rosewall webproject]$
>
>
>
> #!/usr/local/ActivePerl-5.6/bin/perl5.6.1 -w
>
> use Tk;
> my $main=new MainWindow;
> $menubar= $main->Frame(-relief=>"raised",
>-borderwidth=>2);
>
> $filebutton=$menubar->Menubutton;(-text="File",
> -underline=>0);

You have a semicolon betwen Menubutton and (-text etc.), and the = should be
=>.

Also, you might want to quote your switches like this:

$menubar= $main->Frame('-relief'=>"raised",
'-borderwidth'=>2);

Tagore Smith


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




Re: breaking a string into chunks (was Re: help with my parser program?)

2002-03-09 Thread Tagore Smith

Jeff 'japhy' Pinyan wrote:

> 
>   $string =~ s/(.{1,78})\s/$1\n/g;
> 

I played around with this a bit and came up with something similar:

$line=~ s/(.{1,78})(\Z|\s)/$1\n/g;

I think 

$string =~ s/(.{1,78})\s/$1\n/g;

assumes that the string ends in whitespace.

Tagore Smith



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




Re: How to thread in Perl?

2002-03-25 Thread Tagore Smith

Ahmed Moustafa wrote:

> So, how can a new different process by forked? Or, how a function be
> called and the next step execute without waiting for the previous
> function to terminate?

   For your original question (threads) see perldoc perlthrtut.

   When you fork a child process the call to fork returns the child pid to
the parent and 0 to the child. So you can write code that tests the return
value and does one thing in the parent process (continue forking children to
deal with the rest of your files) and another in the child process (process
the current file). You can also use exec to execute another process which is
not a "clone" of the parent.

   See perldoc -f fork and perldoc -f exec.

   Out of curiosity, why do you want to do this?

Tagore Smith


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




Re: How to thread in Perl?

2002-03-25 Thread Tagore Smith

Ahmed Moustafa wrote:

> Tagore Smith wrote:
>
> > Ahmed Moustafa wrote:
> >
> >
> >>So, how can a new different process by forked? Or, how a function be
> >>called and the next step execute without waiting for the previous
> >>function to terminate?
> >>
> >
> >For your original question (threads) see perldoc perlthrtut.
> >
> >When you fork a child process the call to fork returns the child pid
to
> > the parent and 0 to the child. So you can write code that tests the
return
> > value and does one thing in the parent process (continue forking
children to
> > deal with the rest of your files) and another in the child process
(process
> > the current file). You can also use exec to execute another process
which is
> > not a "clone" of the parent.
> >
> >See perldoc -f fork and perldoc -f exec.
> >
> >Out of curiosity, why do you want to do this?
>
>
> The main loop looks for files which are sent via FTP. Once a file is on
> the server, it should be encrypted. The encryption process is slow so
> some files stay as plain text waiting for their turns to be found and
> encrypted.

   That's plausible :). I was worried you might be thinking about doing this
to large pre-existing directory trees.

   If the parent process is solely devoted to looking for and encrypting
these files you may find that using separate processes to do this doesn't
help much, and may in fact hurt- but I don't know everything about your
situation, so then again it may be warranted. I'd stay away from threads
unless you have a lot of time to devote to this, but using fork should be
pretty straightforward (assuming your platform is Unix-like - process
creation is more expensive under Windows).

Tagore Smith



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




Re: Creating a Unique Key

2002-03-28 Thread Tagore Smith


Nikola Janceski wrote:


> Uh... exactly what are you going to be using it for?
> You might want to check out the function call crypt() in the perlfunc
pages.
>
> > -Original Message-
> > From: Gregory Matthews [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday, March 28, 2002 2:30 PM
> > To: [EMAIL PROTECTED]
> > Subject: Creating a Unique Key
> >
> >
> > What is the best way to create a unique, almost impossible to guess,
> > KEY, i.e., ftu880oli88UI8flpq, which can in turn be used as part of a
> > security string, i.e., username: ftu880oli88UI8flpq ?

Check out:


http://www.cs.cornell.edu/People/egs/syslunch-spring02/syslunchsp02/webauth_
tr.pdf

It details how the authors weakened or broke the authentication mechanisms
of a number of prominent e-commerce sites, and gives background information
on doing authentication well. One of the things it talks about is the
weakness of the authentication scheme at the Wall Street Journal's web page
caused by a misunderstanding of how crypt works.

They do propose a stronger authentication scheme.

Tagore Smith




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




Re:client side authentication link

2002-03-28 Thread Tagore Smith

   Someone mailed me off list and pointed out that the url I posted had been wrapped. I
also noticed that I had given a url I had bookmarked which is a mirror of the document,
not the official site. There is some other related material there and postscript and 
html
versions in addition to the pdf. You can find it at:

http://cookies.lcs.mit.edu/

  Sorry for posting twice, but I think this is worth reading if you are doing user
authentication, and it's pretty entertaining as well.

Tagore Smith


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