[10510] in Perl-Users-Digest
Perl-Users Digest, Issue: 4102 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 29 11:06:31 1998
Date: Thu, 29 Oct 98 08:01:32 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 29 Oct 1998 Volume: 8 Number: 4102
Today's topics:
pass array to sub dwinslow@my-dejanews.com
Re: pass array to sub (Matthew Bafford)
Re: pass array to sub dave@mag-sol.com
Re: pass array to sub (Brand Hilton)
Re: pass array to sub dwinslow@my-dejanews.com
Re: Perl & Y2K - booby trap code (Paul Murray)
programing fun: trees: Level <xah@best.com>
Re: psychology of language choice (was Re: language war <jdporter@min.net>
Re: Python fun <jdporter@min.net>
Re: Trapping the output of a system call... <earlw@kodak.com>
Using Perl, How do I convert an Excel worksheet to a HT <majid.sharif@mci.com>
Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 29 Oct 1998 13:50:10 GMT
From: dwinslow@my-dejanews.com
Subject: pass array to sub
Message-Id: <719rqi$u0p$1@nnrp1.dejanews.com>
I am new to perl and am learning by using. But I am stuck. Here is the
problem I have the following subroutine that removes duplicates from an
array. The problem is that I can't figure out how to pass the array to the
sub and get it returned. I would like to do something like this
@links = &remove_dups(@links); # replacing the @links array with the same
array # minus all the duplicates.
sub remove_dups { foreach $nextlink (sort @______) { #What goes here? if
($nextlink eq $firstlink) { } else {
push(@newlinks,$nextlink); $firstlink = $nextlink; # how do i return
the new array? } } } By the way, If there is an easier way to remove
dups from an array, that would also be helpful, but I would still like to
know how to do the previous. TIA David
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Thu, 29 Oct 1998 09:58:43 -0500
From: dragons@scescape.net (Matthew Bafford)
Subject: Re: pass array to sub
Message-Id: <MPG.10a24cb27f79532c9896ea@news.scescape.net>
In article <<719rqi$u0p$1@nnrp1.dejanews.com>>, dwinslow@my-
dejanews.com (dwinslow@my-dejanews.com) pounded the following:
=> I am new to perl and am learning by using. But I am stuck. Here is the
=> problem I have the following subroutine that removes duplicates from an
=> array. The problem is that I can't figure out how to pass the array to the
=> sub and get it returned. I would like to do something like this
=>
=> @links = &remove_dups(@links); # replacing the @links array with the same
=> array # minus all the duplicates.
!TIMTOWTDI!
#!/usr/bin/perl -w
use strict;
my ( @array1, @array2, @array3 );
@array1 = @array2 = @array3 =
qw/just another perl hacker perl just another hacker/;
print "Array1 Before: ", join(" ", @array1), "\n";
print "Array2 Before: ", join(" ", @array2), "\n";
print "Array3 Before: ", join(" ", @array3), "\n";
@array1 = remove_dups1( @array1);
remove_dups2(\@array2);
@array3 = remove_dups3(\@array3);
print "Array1 After: ", join(" ", @array1), "\n";
print "Array2 After: ", join(" ", @array2), "\n";
print "Array3 After: ", join(" ", @array3), "\n";
# Method 1, @array is expanded and passed to the function as @_
sub remove_dups1 {
my %dups;
@dups{@_} = ();
keys %dups;
}
# Method 2, @array is passed as a reference (in the first element
# of @_ (ie: $_[0]). We modify @array directly.
sub remove_dups2 {
my %dups;
@dups{@{$_[0]}} = ();
@{$_[0]} = keys %dups;
}
# Method 2, @array is passed as a reference (in the first element
# of @_ (ie: $_[0]). We don't modify @array.
sub remove_dups3 {
my %dups;
@dups{@{$_[0]}} = ();
keys %dups;
}
__END__
=> [snip]
=> By the way, If there is an easier way to remove
=> dups from an array, that would also be helpful, but I would still like to
=> know how to do the previous.
The method I used above is just one of many! See perlfaq4 for more
details. (Hint: search for unique).
=> TIA
Hope This Helps!
=> David
--Matthew
------------------------------
Date: Thu, 29 Oct 1998 14:52:17 GMT
From: dave@mag-sol.com
Subject: Re: pass array to sub
Message-Id: <719vf1$2gt$1@nnrp1.dejanews.com>
In article <719rqi$u0p$1@nnrp1.dejanews.com>,
dwinslow@my-dejanews.com wrote:
> I am new to perl and am learning by using. But I am stuck. Here is the
> problem I have the following subroutine that removes duplicates from an
> array. The problem is that I can't figure out how to pass the array to the
> sub and get it returned. I would like to do something like this
Cleaning up your code a bit (well, a lot really)...
@links = &remove_dups(@links); # replacing the @links array with the same
# array minus all the duplicates.
sub remove_dups
{
my @list = @_; # Parameters to a sub are passed in the
# special array @_
foreach $nextlink (sort @list)
{
if ($nextlink eq $firstlink)
{
}
else
{
push(@newlinks,$nextlink);
$firstlink = $nextlink;
}
}
return @list; # You can directly return a list from a sub
}
> By the way, If there is an easier way to remove
> dups from an array, that would also be helpful, but I would still like to
> know how to do the previous. TIA David
sub remove_dups
{
my %check;
@check{@_} = @_;
return keys %check;
}
hth,
Dave...
--
dave@mag-sol.com
London Perl M[ou]ngers: <http://london.pm.org/>
[Note Changed URL]
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: 29 Oct 1998 15:04:40 GMT
From: bhilton@tsg.adc.com (Brand Hilton)
Subject: Re: pass array to sub
Message-Id: <71a068$9qv10@mercury.adc.com>
In article <719rqi$u0p$1@nnrp1.dejanews.com>,
<dwinslow@my-dejanews.com> wrote:
>I am new to perl and am learning by using. But I am stuck. Here is the
>problem I have the following subroutine that removes duplicates from an
>array. The problem is that I can't figure out how to pass the array to the
>sub and get it returned. I would like to do something like this
>
>@links = &remove_dups(@links); # replacing the @links array with the same
>array # minus all the duplicates.
>
>sub remove_dups { foreach $nextlink (sort @______) { #What goes here? if
>($nextlink eq $firstlink) { } else {
>push(@newlinks,$nextlink); $firstlink = $nextlink; # how do i return
>the new array? } } } By the way, If there is an easier way to remove
>dups from an array, that would also be helpful, but I would still like to
>know how to do the previous. TIA David
Yech! You really need to figure out what happened to your line
wrapping before posting again.
As is covered in the perlsub documentation, all subroutines take a
list of scalars as parameters and return a list of scalars. The
parameters passed to your subroutine are in @_. When you call return,
just give it an array.
BTW, you might want to read perlfaq4. Specifically, the section
titled, "How can I extract just the unique elements of an array?"
--
_____
|/// | Brand Hilton bhilton@adc.com
| ADC| ADC Telecommunications, ATM Transport Division
|_____| Richardson, Texas
------------------------------
Date: Thu, 29 Oct 1998 15:33:10 GMT
From: dwinslow@my-dejanews.com
Subject: Re: pass array to sub
Message-Id: <71a1rn$602$1@nnrp1.dejanews.com>
In article <719vf1$2gt$1@nnrp1.dejanews.com>,
dave@mag-sol.com wrote:
> In article <719rqi$u0p$1@nnrp1.dejanews.com>,
> dwinslow@my-dejanews.com wrote:
> > I am new to perl and am learning by using. But I am stuck. Here is the
> > problem I have the following subroutine that removes duplicates from an
> > array. The problem is that I can't figure out how to pass the array to the
> > sub and get it returned. I would like to do something like this
>
> Cleaning up your code a bit (well, a lot really)...
>
> @links = &remove_dups(@links); # replacing the @links array with the same
> # array minus all the duplicates.
>
> sub remove_dups
> {
> my @list = @_; # Parameters to a sub are passed in the
> # special array @_
>
> foreach $nextlink (sort @list)
> {
> if ($nextlink eq $firstlink)
> {
> }
> else
> {
> push(@newlinks,$nextlink);
> $firstlink = $nextlink;
> }
> }
>
> return @list; # You can directly return a list from a sub
> }
>
> > By the way, If there is an easier way to remove
> > dups from an array, that would also be helpful, but I would still like to
> > know how to do the previous. TIA David
>
> sub remove_dups
> {
> my %check;
>
> @check{@_} = @_;
>
> return keys %check;
> }
>
> hth,
>
> Dave...
>
> --
> dave@mag-sol.com
> London Perl M[ou]ngers: <http://london.pm.org/>
> [Note Changed URL]
>
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
>
Dave,
sorry about the code. I assure you it didn't look like that when posting.
Thanks for the help.
David
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: 29 Oct 1998 14:56:02 GMT
From: murray-paul@usa.net (Paul Murray)
Subject: Re: Perl & Y2K - booby trap code
Message-Id: <slrn73h0g1.5qq.murray-paul@unix3.netaxs.com>
On 22 Oct 1998 02:58:50 -0700, Russ Allbery <rra@stanford.edu> wrote:
>Randal Schwartz <merlyn@stonehenge.com> writes:
>>>>>>> "Snowhare" == Snowhare <snowhare@devilbunnies.org> writes:
>> Snowhare> I concur with the basic premise. And it is _very_ applicable
>> Snowhare> to Perl. It wasn't until the Blue Camel came out that the
>> Snowhare> reference book stated that localtime returns the year -
>> Snowhare> 1900. The Pink Camel states something like "comes straight out
>> Snowhare> of a tm struct (That's a bit of C programming lingo - don't
>> Snowhare> worry about it.)" (pg 160) :O
>> Bulloney. Nonsense. "straight out of a tm struct" was completely
>> apropos to the audience of the Pink camel... other Unix hackers that
>> knew C. And nearly all of them were trusted to be able to type "man
>> localtime" to get the details.
>Then why does the text of the book strongly imply that they shouldn't
>bother looking it up in the man page? Or is Snowhare's quote incorrect?
The exact quote is as Showhare said:
'All array elements are numeric, and come straight out of a *struct tm*.
(That's a bit of C programming lingo - don't worry about it.)'
Pink Camel p160 ( *'s used to indicate bold in the original)
-Paul Murray
------------------------------
Date: Thu, 29 Oct 1998 05:03:13 -0700
From: "Xah" <xah@best.com>
Subject: programing fun: trees: Level
Message-Id: <36386639$0$29765@nntp1.ba.best.com>
Here's another fun programing problem on tree structures. This one is probably the most difficult among those I posted before. The problem spec is attached below my sig. Have fun.
I'll be posting my solution this Saturday or Sunday.
Xah, xah@best.com
http://www.best.com/~xah/PageTwo_dir/more.html
PS if you need a hard-wrapped version of this post, please email me.
#_____ Level _____ _____ _____ _____
=pod
B<Level>
Level(tree, [n]) returns a list of all nodes that are on the nth level of the tree. Level 0 is the root, consisting of the whole tree. Level 1 is the immediate elements in the list, i.e. those accessible by $tree->[m1]. Level 2 are those accessible by $tree->[m1][m2], and so on.
If n is negative, levels are counted from leaves to root. That is, Level(tree, [-n]) for a positive n returns all nodes in the tree that have depth n. The depth of a tree, Depth(tree), is the maximum number of indices needed to specify any node, plus one. (see Depth.)
Level( tree, [n, m]) returns a list of all nodes of the tree that's on levels n to m inclusive. Either one of n or m can be negative. For example, Level( tree, [2,-2]) returns all nodes on level 2 or more, and has depth 2 or greater.
Level( tree, n) is equivalent to Level( tree, [1, n]).
In general, the form is Level( tree, levelspec), where levelspec has one of the forms: n, [n], [n, m].
If the levelspec is beyond the depth of the tree, or if it doesn't make sense (e.g. [5,3] or [-3,-5]), an empty list is returned. (i.e. []). Note: levelspec such as [3,-2] is not necessarily illegal.
Nodes in the result list is ordered by their position indexes' components ascending (i.e. [2,1] comes before [3].). When there is a tie (e.g. [2] vs. [2,1]), index length descending is used (i.e. [2,1] comes before [2]). For example, here's a complete index set sorted the way Level would: [[0],[1,0],[1,1,0],[1,1,1],[1,1,2],[1,1],[1,2,0],[1,2,1],[1,2,2],[1,2],[1],[2,0],[2,1,0],[2,1,1],[2,1,2],[2,1],[2,2,0],[2,2,1],[2,2,2],[2,2],[2]]. See IndexSetSort for details about sorting nodes.
Related: Part, Extract.
Example:
Level( $tree, [0]); # returns [ $tree ].
Level( $tree, [1]); # returns $tree unchanged.
Level( [[1,2],[3,[44]],'a'], [2]); # returns [ 1,2,3,[44] ].
Level( [[1,2],[3,[44]],'a'], [3]); # returns [ 44 ].
Level( [[1,2],[3,[44]],'a'], [-1]);
# returns all the leaves [ 1,2,3,44,'a' ]. In other words, atoms or leaves are those having depth 1.
Level( [[1,2],[3,[44]]], [-2]);
# returns [ [1,2], [44] ] because both elements has depth 2.
Level( [[1,2],[3,[44]]], [-3]); # returns [ [3,[44]] ]
Level( [[1,2],[3,[44]]], [-4]);
#returns [ [[1,2],[3,[44]]] ] because the whole tree has depth 4.;
Level( [[1,2],[3,[44]]], [-5]);
# returns [ ] because the tree has depth 4. i.e. It's root node has depth 4. No other node can have depth greater than the root.;
Level( [[1,2],[3,[44]]], [1,2]);
# returns [ 1, 2, [1,2], 3, [44], [3,[44]] ]
# the result consists of all nodes that requires 1 or 2 indexes to access.
# their position indexes in the tree are:
# [0,0], [0,1], [0], [1,0], [1,1], [1]
# note the order returned.
Level( [[1,2],[3,[44]]], [1,3]);
# returns [ 1, 2, [1,2], 3, 44, [44], [3,[44]] ]
# these are nodes on levels 1 to 3.
# their position indexes are
# [0,0], [0,1], [0], [1,0], [1,1,0], [1,1], [1]
Level( [[1,2],[3,[44]]], [2,3]);
# returns [ 1, 2, 3, 44, [44] ]
# their position indexes are
# [0,0], [0,1], [1,0], [1,1,0], [1,1]
Level( [[1,2],[3,[44]]], [2,-1]);
# returns [ 1, 2, 3, 44, [44] ]
Level( [[1,2],[3,[44]]], [2,-2]);
# returns [ [44] ] because [44] is the only node on level 2 or greater and have a depth of 2 or more.
Level( [[1,2],[3,[44]]], [1,-2]);
# returns [ [1,2], [44], [3,[44]] ]
# these are nodes on level 1 or greater and have a depth of 2 or more.
Level( [[1,2],[3,[44]]], [-10,-2]);
# returns [ [1,2], [44], [3,[44]], [[1,2],[3,[44]]] ]
# i.e. all nodes having depth <= 10 and >= 2. Their depths are 2, 2, 3, 4.
Level( [[1,2],[3,[44]]], [-2,1]);
# returns [ [1,2] ];
# i.e. all nodes having depth <= 2 and on level <= 1.
Level( [[1,2],[3,[44]]], [-2,2]);
# returns [ 1,2,[1,2],3,[44] ];
# i.e. all nodes having depth <= 2 and on level <= 2.
Level( [[1,2],[3,[44]]], [-2,3]);
# returns [1,2,[1,2],3,44,[44]];
# i.e. all nodes having depth <= 2 and on level <= 3.
=cut
------------------------------
Date: Thu, 29 Oct 1998 10:33:09 -0500
From: John Porter <jdporter@min.net>
Subject: Re: psychology of language choice (was Re: language war ...)
Message-Id: <36388AB5.C4C0D2A8@min.net>
Jason Orendorff wrote:
>
> Interestingly, neither Perl nor Python has "true" global
> variables (anymore) in the same sense as C has 'em.
Perl has some true globals, but they're "special". E.g. %ENV.
Perl's "package" globals are truly global in the sense that they are
visible from everywhere. You might have to qualify the name, though.
Namespaces are not protected.
So maybe that's not the same as in C. I'm not sure I'd agree that C's
way of doing things should be called the "true" way.
John Porter
------------------------------
Date: Thu, 29 Oct 1998 10:38:33 -0500
From: John Porter <jdporter@min.net>
Subject: Re: Python fun
Message-Id: <36388BF9.F0EC38D6@min.net>
Jason Orendorff wrote:
>
> > > Is map2 a recent addition to the perl language or a user defined
> > > function?
> >
> > It's one that I could write in Perl. That's the point.
>
> Well, you *could* do it, but it might not be pretty. You'd have to
> use either references or typeglobs, I think. Hopefully references.
>
> In Tcl, ML, Python, or Lisp, you'll find it's much more fun. Sorry
> to say it, 'cos I like Perl too... but it's true.
Speak for yourself, monkey boy. :-)
# iterate over the cartesian product of two vectors.
sub map2(&$$) {
my( $code, $a1, $a2 ) = @_;
my @result;
for my $i1 ( @$a1 ) {
for my $i2 ( @$a2 ) {
push @result, $code->( $i1, $i2 )
}
}
@result
}
That's pretty enough for me.
> def map_cartesian_product_2(fn, list1, list2):
> vals = []
> for item in list1:
> for item2 in list2:
> vals.append(fn(item, item2))
> return vals
Hmm, yes, that is the canonical equivalent of the code I had.
John Porter
------------------------------
Date: Thu, 29 Oct 1998 09:54:36 -0500
From: Earl Westerlund <earlw@kodak.com>
Subject: Re: Trapping the output of a system call...
Message-Id: <363881AC.5FC4@kodak.com>
Rob Bridal wrote:
>
> Hello,
>
> i know I should already know this, but I can't find it anywhere. I
> checked the PerlDocs and searched the newsgroup. I need a way to trap
> the output of a system call, such as a whois. I know system() and
> exec() won't work for trapping it.
>
> Thank you in advance,
> Rob Bridal
Check out backticks.
--
+-----------------+----------------------------------------+
| Earl Westerlund | Kodak's Homepage: http://www.kodak.com |
+-----------------+----------------------------------------+
| The opinions expressed herein are mine and mine alone |
| (most people don't seem to want them anyway) |
+----------------------------------------------------------+
------------------------------
Date: Thu, 29 Oct 1998 10:42:13 -0500
From: "Majid Sharif" <majid.sharif@mci.com>
Subject: Using Perl, How do I convert an Excel worksheet to a HTML file?
Message-Id: <P00_1.495$%m6.3340@news.cwix.com>
With the simple code below as a starting point, how do convert an Excel
worksheet to a HTML file?
The requirements are to simply open a Perl script from a DOS prompt that
opens an existing Excel workheet and converts the file to an HTML file to
later be uploaded to the Internet.
******************************************
use OLE;
$excel = CreateObject OLE 'Excel.Application' or warn "Couldn't create new
instance of Excel App!!";
$excel->Workbooks->Open( 'c:\test.xls' );
$excel->Workbooks(1)->Worksheets('Sheet1')->Cells(1,2)->{Value} = 'Majid';
$excel->Workbooks(1)->Worksheets('Sheet1')->Cells(1,2)->{Value} = 'Sharif';
$excel->Save();
$excel->Quit();
------------------------------
Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>
Administrivia:
Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.
If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu.
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 4102
**************************************