[7284] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 909 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Aug 23 01:27:15 1997

Date: Fri, 22 Aug 97 22:01:32 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 22 Aug 1997     Volume: 8 Number: 909

Today's topics:
     Re: beginner's help <ajohnson@gpu.srv.ualberta.ca>
     Re: beginner's help <arv@fcee.urv.es>
     Re: Building a (very?) complex data structure.... (Rob Bedell)
     Exception Handling in OO Perl <ajiteshd@wiproge.med.ge.com>
     Getting index of array element (Eric Penn)
     Re: How do I find the system date/time? (Danny Aldham)
     Re: Novice scratches head (Andrew M. Langmead)
     Re: Problem with opening file for appending.....ARGH!!! (Tad McClellan)
     Re: Proprietary Perl ??? (O'Reilly Perl Resource Kit) <merlyn@stonehenge.com>
     Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 22 Aug 1997 22:14:38 -0500
From: Andrew Johnson <ajohnson@gpu.srv.ualberta.ca>
To: frankel@cs.concordia.ca
Subject: Re: beginner's help
Message-Id: <33FE559E.2B2CB417@gpu.srv.ualberta.ca>

frankel@cs.concordia.ca wrote:
> 
> I tried to use the hex function to convert hexadecimal to decimal.
> Since hex() doesn't recognize negatif number, I check for it and I just
> delete it.
> Here's the code :
> 
> #!/site/bin/perl
> print "Enter hexadecimal number: ";
> $answer=<STDIN>;
>  if ($answer= ~/^-/) {
              ^^^
   if ($answer =~ /^-/) {   # no space between =~

>         print "-";
>         $answer= ~s/^-/""/;
                  ^      ^^ and don't want these quotes
          $answer=~s/^-//;

> }
> print hex($answer),"\n";

you can shorten the whole thing to:
#!/site/bin/perl -w
chomp($answer=<STDIN>);
print "-" if $answer=~s/^-//;
print hex($answer),"\n";

regards
andrew


------------------------------

Date: Sat, 23 Aug 1997 00:17:13 +0200
From: Alexis Roda <arv@fcee.urv.es>
To: frankel@cs.concordia.ca
Subject: Re: beginner's help
Message-Id: <33FE0FE9.49735BBE@fcee.urv.es>

frankel@cs.concordia.ca wrote:
> 
> I tried to use the hex function to convert hexadecimal to decimal.
> Since hex() doesn't recognize negatif number, I check for it and I just
> delete it.
> Here's the code :
[...]

There are some mistakes:
* PERL interprets $answer=<space>~/^-/ in a very funny way: first it
tries to match $_ against the pattern, then calculates the bitwise
negation of the truth value returned by the pattern matching and finally
assigns this value to $answer.

* if I'm right, when you enter -1 $answer gets ""1 not just 1, so
hex($answer) will evalutate to zero.

Not a flame, just a hint: maybe some prints to see $answer's "evolution"
could have helped you to detect what were wrong.


Saludos
Alexis Roda


------------------------------

Date: Sat, 23 Aug 1997 04:08:06 GMT
From: zen@onyourmark.com (Rob Bedell)
Subject: Re: Building a (very?) complex data structure....
Message-Id: <5tlntb$3vh@newsops.execpc.com>

The illustrious toutatis@remove.this.toutatis.net (Toutatis)
illuminated our situation by stating:

The following is a simple subroutine I use to access these "hash
heirarchies", as I call them:

######################################################################
# traverse_heir
#   traverse all the nodes of a heirarchy, calling
#   $before beforing recursing, $after after returning from the
#recurse,
#   and $leaf for each leaf node.
# Notes: traverses them in a sorted order.
#        processes parent nodes before child nodes.
######################################################################
sub traverse_heir {
  local($ref,$before,$after,$leaf,@args) = @_;
  local($i);

  foreach $i (sort keys %{$ref}) {
    if(ref  ${$ref}{$i} eq "HASH") {     #node w/children, so recurse
      &{$before}($i,${$ref}{$i},@args); #process this node.
      traverse_heir(${$ref}{$i},$before,$after,$leaf,@args);
      &{$after}($i,${$ref}{$i},@args);  #process this node.
    } else {    #leaf node.
      &{$leaf}($i,${$ref}{$i},@args);
    }
  }
}

__END__

This should help with accessing these structures.
Note that it is trivial to add conditional recursion.  Just wrap the 
&{$before}... in an if stmt that only recurses when $before
returns true.
Below is a sample declaration of a hash heirarchy:

%heirarchy = (
  "name1" => {
     "this node has no children" => "",
     "this node does" => {
         "la" => "fa so mi la do",
     },
  },
  "name2" => {
     "here we go again" => "",
     "__name2__" => "possible way to store data you might normally"
			. " just associate with \$heirarchy{name2}",
  }
);

__END__

If you need to store data in a particular node that has children, come
up with some example which is highly unlikely to be referenced
(prefix it with a '/' in unix, for instance, or character \0xFF).

The following would print the contents of the %heirarchy hash
heirarchy, for instance (note the use of the global, the sub could
be rewritten slightly to call each function with an array reference
so that the subs can manipulate what the children will be called with)

$leader = 0;
&traverse_heir(
  \%heirarchy,   #note that this is a hash table reference
  sub {  #called before traversing the children
     local($key,$val_ref,@args) = @_;
     print "  " x $leader, "$key: \n";
     $leader++;
  },
  sub { #called after the children have been recursed through
     $leader--;
  },
  sub { #called for each leaf node
     local($key,$value,@args) = @_;
     print "  " x $leader, "$key => $value\n";
  },
  #no extra args to be passed to the various anon subs.
);

This is for illustration purposes, of course.  A better implementation
might tighten it up a little and make the output cleaner. If you're
wondering, I have checked all the code in this post with perl -c -w,
so it should work nicely for you.

Here is the output of the above snippet (if you combine all the code
sections in this post into one, you should get the same results):

name1:
  this node does:
    la => fa so mi la do
  this node has no children =>
name2:
  __name2__ => possible way to store data you might normally just
associate with $heirarchy{name2}
  here we go again =>

The __name2__ value may wrap on your display.

============
If you want I'll clean up the code I have to create
hash heirarchies out of directory heirarchies and email
it to you.

In order to access a single entry, just deref the entire chain of
hash's:

print $heirarchy{"name1"}{"this node does"}{"la"};
yields:
fa so mi la do

NOTE: The recursion technique used here
would be faster using iteration instead.  perl
is a bit slow when it comes to function calls, with
a lot of stack manipulation going on.  Recursion
just adds to the calls already being made.  It also
has the potential to waste a lot of memory if you get
stuck in an infinite loop.  An iteration technique
that didn't involve a heirarchy without parental
references would probably have a stack to
maintain it's position, so maybe it wouldn't help that
much anyway ;)

>The key-field comes from a database, and contains up to 6 fields. The objects
>contain various information.

You might use pipe-delimiters, or some uncommon character like 0xFF
or null, to separate the data.

>How do I put these values, sorted per catagory in a datastructure, and, as
>important, how do I access them again.

The implementation I gave you will sort the data by key.

Say thank you if you find this useful ;)




------------------------------

Date: Fri, 22 Aug 1997 10:33:26 -0530
From: Ajitesh Das <ajiteshd@wiproge.med.ge.com>
Subject: Exception Handling in OO Perl
Message-Id: <33FDB84E.41C6@wiproge.med.ge.com>

Hi All,
  I am currently scriptin' a "Internet Showcase" script. 
where I came in to a situation to USE EXCEPTION HANDLER 
in OO-PERL.
  Does anybody has any sample script or know some site location
relating to that?
   Thanks for your help in advance 

Rgds
  Ajitesh
            [ p.s: During posting ur reply pl mail me a 
                   copy ]

----
  Ajitesh Das                    ajiteshd@wiproge.med.ge.com
---

         "Golden Rule: never derive from a concrete class "


------------------------------

Date: Sat, 23 Aug 1997 04:33:52 GMT
From: stupid@wco.com (Eric Penn)
Subject: Getting index of array element
Message-Id: <33fe661d.99291655@news.wco.com>

Let's suppose that I have an array of unique values.  The order of the
values in significant.  Now, say I'd like to find the index of one value in
the array.  The  way I've come up with is a brute force search, which to me
seems both ugly and slow:

@ary = (0,1,2,3,4,5,6,7,8,9);
for (0..@ary) {
	if ($ary[$_] == 7) {
		$idx = $_;
		last;
	}
print "the index of 7 is $idx\n";

For a small array this seems fine, but if I have several hundred values to
step through, I would think that there must be a better way.

--
   Eric Penn                                STUPID's three rules to life:
  stupid@wco.com                             Stick with what you're good at,
 http://www.wco.com/~stupid/                  Learn from your mistakes, and
"stoo"                                         When in doubt, act stupid!


------------------------------

Date: 22 Aug 1997 20:45:19 -0700
From: danny@lennon.postino.com (Danny Aldham)
Subject: Re: How do I find the system date/time?
Message-Id: <5tlmcf$a3j$1@lennon.postino.com>

Kelly Baxter (dewolf@teleport.com) wrote:
: I tried this and it didn't work...  I'm in NT though... is this a unix
: variable? (localtime that is)

I am using perl5.003_07 from activeware and localtime works fine.

--
Danny Aldham           SCO Ace , MCSE , JAPH , DAD
I don't need to hide my e-mail address, I broke my sendmail.


------------------------------

Date: Fri, 22 Aug 1997 23:21:35 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Novice scratches head
Message-Id: <EFC8vz.L5G@world.std.com>

"Andrew C. Risehnoover" <qball@ti.com> writes:

>Another trick you can try is 'casting' $x.  before the compare, add in
>this line:$x*=1;
>This will force perl to look at it like a number.

In all but the most obscure places, (bitwise boolean operators come to
mind) all you need to "cast" the variable into number is to use it as
a number.

"*=1" will not cause $x to look any more numerically equal to 100000
than without.



-- 
Andrew Langmead


------------------------------

Date: Fri, 22 Aug 1997 18:04:34 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Problem with opening file for appending.....ARGH!!!!!
Message-Id: <2u5lt5.g54.ln@localhost>

David Eck (daveck@top.monad.net) wrote:
: Hi,
: I am having a problem with the following code.  It is driving me crazy.
: I am a novice so if its something obvious, go easy on me.

: I have confirmed that the value of $filename is what it should be.  Yet
: it does not open the file.  When I replace $filename with the static
: text of the actual file name it works fine.  This is driving me crazy,
: so if anyone could help, I would appreciate it.  


:    open FOLDER, '>>$filename' or

This is trying to open a file whose name has these nine characters:

$-f-i-l-e-n-a-m-e
1 2 3 4 5 6 7 8 9


If you want the $filename variable to be interpolated, then use
double quotes instead of single ones.


:      &CgiDie("Could not open file - ($filename)");

: Thanks,

You're welcome.


--
    Tad McClellan                          SGML Consulting
    tadmc@flash.net                        Perl programming
    Fort Worth, Texas


------------------------------

Date: 22 Aug 1997 20:11:09 -0700
From: Randal Schwartz <merlyn@stonehenge.com>
To: Michael Shael O'Neill Selden <selden@instinet.com>
Subject: Re: Proprietary Perl ??? (O'Reilly Perl Resource Kit)
Message-Id: <8c3eo161bm.fsf@gadget.cscaper.com>

>>>>> "Michael" == Michael Shael O'Neill Selden <selden@NOSPAM.instinet.com> writes:

Michael> 	I was checking out the O'Reilly site for information
Michael> regarding the upcoming Advanced Perl Programming book (totem
Michael> animal shock!)  when I came across a page detailing an
Michael> O'Reilly product due out in 10/97 called the Perl Resource
Michael> Kit (http://perl.oreilly.com/), the blurb for which indicates
Michael> that it includes new language features
Michael> ("commercially-enhanced Perl compiler, written by Larry Wall,
Michael> creator of Perl", "enhanced database support tools",
Michael> "enhanced Perl security modules", etc.).  What I am curious
Michael> about is (in addition to details regarding the new features,
Michael> which I have asked O'Reilly for :-}) what the impact of this
Michael> development (no pun intended) will be on the P.D.  Perl
Michael> world.  In other words, is this going to result in a
Michael> discrepancy between the Perl used by paid-in-full corporate
Michael> accounts and your average download and code type.  Curiosity
Michael> overwhelms me; I seek enlightenment.

I cannot speak for ORA.  However, having just heard what I consider
to be a public demo of said product, and Tim O'Reilly's personal
response to a question similar to the one you post at the just completed
Perl Conference, let me paraphrase what he said:

    If it's "core perl", it'll be free, even if Larry develops it on
    company time.  If it's something that ORA can provide as "value
    added", it *might* cost money (a little or a lot).  However, they're
    not yet (publicly) decided on whether the perl-java interface will be
    free or commercial.  Perl itself will continue to be free.

I had many opportunities to interact with Tim and Larry in the past
few days.  As the co-founder of The Perl Institute, I can assure you
that Perl is in good hands, and will continue to remain Free,
Available, and Useful, and that ORA is our partner in making that
happen, not our opponent.

Put your pitchforks away, please. :-)

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 374 more days
## before I go to *prison* for 90 days; email fund@stonehenge.com for details

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@ora.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me


------------------------------

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V8 Issue 909
*************************************

home help back first fref pref prev next nref lref last post