[7223] in Perl-Users-Digest
Perl-Users Digest, Issue: 849 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Aug 11 20:08:00 1997
Date: Mon, 11 Aug 97 17:01:36 -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 Mon, 11 Aug 1997 Volume: 8 Number: 849
Today's topics:
Re: Problem With Join() <rootbeer@teleport.com>
Re: Problem With Join() (Jot Powers)
Re: Question on and associative arrays and -w switch (Craig Berry)
Sharing Variables (Arielle Sumits)
Re: signals and EOF (Charles DeRykus)
Re: sleep for a few hundrad milli seconds <armbrust@mcnet.mcpherson.edu>
Re: Sorting this file is killing me (Thomas Wernitz)
Re: splice (Terry Michael Fletcher - PCD ~)
Re: Substring substitution in PATH variable <rootbeer@teleport.com>
Re: textarea - Script not producing line feeds?? (Jason Nugent)
the port of GD graphic library module for NT alpha ? <htkim@wsn.co.kr>
url_get undefined subroutine (Eric Bresie)
Re: Visual representation of UNIX Directories? (Brian Wheeler)
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 11 Aug 1997 14:43:00 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: perrella andrew c <perrella@ehsn9.cen.uiuc.edu>
Subject: Re: Problem With Join()
Message-Id: <Pine.GSO.3.96.970811143614.27045E-100000@julie.teleport.com>
On Mon, 11 Aug 1997, perrella andrew c wrote:
> open(admin_info, "./admin/ip-admin.dat");
You should really be checking the return value of open. And if you used
-w, Perl would remind you that a filehandle shouldn't be all lower case. I
recommend all caps for filehandle names.
> undef $/;
> $admin_data = <admin_info>;
> $/ = '\n';
That's probably not what you want, since '\n' doesn't appear in files so
often as "\n" does. :-)
Hope this helps!
--
Tom Phoenix http://www.teleport.com/~rootbeer/
rootbeer@teleport.com PGP Skribu al mi per Esperanto!
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: 11 Aug 1997 22:28:27 GMT
From: news@bofh.com (Jot Powers)
Subject: Re: Problem With Join()
Message-Id: <5so3mb$2ba$1@gazette.corp.medtronic.com>
In article <Pine.SOL.3.95.970811154429.6811A-100000@ehsn9.cen.uiuc.edu>,
perrella andrew c wrote:
>
>Hello,
>
> I have a data file that has information separated by new lines
>
>\n. When I split the data apart into a list using the following code
>everything works fine.
>
>open(admin_info, "./admin/ip-admin.dat");
> undef $/;
> $admin_data = <admin_info>;
> $/ = '\n';
> close(admin_info);
>
> @ip_data = split(/\n/, $admin_data);
You're making this more difficult than it needs to be. (BTW, these
are the kinds of sentences that make Randal happy, because he gets
to come back with some sort of Schwarzian map/sort/qw answer and
say, "You are making it too difficult too". ;)
node127% more data
1
A
2
B
node127% perl -de 1
Loading DB routines from perl5db.pl version 1
Emacs support available.
Enter h or `h h' for help.
main::(-e:1): 1
DB<1> open(DATA,"data") or die "$!\n";
DB<2> @data = <DATA>
DB<3> close(DATA) or die "$!\n";
DB<4> x @data
0 1
1 'A
'
2 2
3 'B
'
> local($new_info) = join("\n", @ip_data);
>
> open (change, ">./admin/ip-admin.dat");
> print change "$new_info";
> close (change);
Well, $new_info is a variable, but since you went ahead and redefined
$/, I would expect it to only print whatever is in $new_info. Let's
see what I get:
DB<5> @new_data = (3,'C');
DB<6> $new_data = join('\n',@data,@new_data)
DB<7> p $new_data
1
\nA
\n2
\nB
\n3\nC
Now, you'll notice I joined two arrays together, since yours didn't
make much sense to me as you were joining something that was already
an array. I must be missing something.
>
>The Above code returns SCALAR(0x83c488) everywhere I would like to have a
>new line
>
>error type 2:
What platform/version? A perl -v might help since I have never
seen an error like that.
>
>Any ideas?
I'd redo the way you're doing it to more like my first stuff. There
is no reason to much with $/ if you don't have to. (Unless you're
trying to get a version of grep fast or something, in which case
mucking with $/ can gain you amazing speed.)
-Jot
--
Jot Powers news@bofh.com
Unix System Administrator
"Sometimes you just have to grab the bull by the tail and face the situation."
------------------------------
Date: 11 Aug 1997 20:39:32 GMT
From: cberry@cinenet.net (Craig Berry)
Subject: Re: Question on and associative arrays and -w switch
Message-Id: <5snta5$juo$1@marina.cinenet.net>
Saviz Artang - MPG ~ (sartang@pcocd2.intel.com) wrote:
: I use the perl -w switch because it catches a lot of my mistakes.
:
: The problem I'm having is that I'm using an associative array to
: represent a hash of a list that I have. I do searches through
: this hash and when things are found every thing is good
: but when things are not found in this associative array the
: -w causes the code to spit out warnings saying an uninitialize
: value was used, since I have an if statement to see if the
: item was there or not:
:
: if ( a{"key"} ) {
: print "found it" ;
: }
: else {
: print "didn't find it";
: }
:
: Now I understand the reason the warning is being produced but
: I want to perform this same thing on my hash for searching instead
: of going through a list linearly. So how can I do that and still
: avoid the warning ??
Yes; test for ( defined $a{"key"} ) instead of just ( $a{"key"} ). Hope
this helps!
---------------------------------------------------------------------
| Craig Berry - cberry@cinenet.net
--*-- Home Page: http://www.cinenet.net/users/cberry/home.html
| Member of The HTML Writers Guild: http://www.hwg.org/
"Every man and every woman is a star."
------------------------------
Date: 11 Aug 1997 21:45:09 GMT
From: arielle@mit.edu (Arielle Sumits)
Subject: Sharing Variables
Message-Id: <arielle-1108971748070001@olc-mac-21.mit.edu>
How can I share variables across several perl scripts?
Thanks,
Arielle
------------------------------
Date: Mon, 11 Aug 1997 21:27:19 GMT
From: ced@bcstec.ca.boeing.com (Charles DeRykus)
Subject: Re: signals and EOF
Message-Id: <EErq9K.DvI@bcstec.ca.boeing.com>
In article <33EB868D.7B7C@corp.airmedia.com>,
Paul S R Chisholm <psrc@corp.airmedia.com> wrote:
> I recently needed to write a persistent process with a configuration
> file. I tried to write it so it would re-read it's configuration file on
> SIGHUP:
>
> # get the original configuration file
> if (! read_config()) {
> die "$0: cannot read configuration file(s)\n";
> }
> # ... and again on SIGHUP
> $SIG{"HUP"} = sub { read_config(); };
>
> (I think I have sigaction, so I don't need to re-catch the signal. I
> don't set the catch function directly to read_config(), for
> compatibility with the final version of the code; see below.)
>
> Well, that seemed fine, except that the program would exit on SIGHUP! It
> took me a little while to realize that the main loop of the program:
>
> while (<STDIN>) {
> # process input
> }
>
> was terminating, having seen EOF (an undefined value from standard
> input) when the read was interrupted by the signal. (Makes sense; the
> underlying read(2) returns -1, with errno=EINTR, which would perculate
> up through the I/O libraries as an error.)
>
> Okay, so one trick is to distinguish <>-undefined-on-true-EOF from
> <>-undefined-on-interrupt-error. The following works:
>
> $SIG{"HUP"} = sub { read_config(); $::saw_signal = 1; };
>
> # look at standard input, send matching lines to e-mail
> $::saw_signal = 0;
> while (1) {
> $_ = <STDIN>;
> if (! defined($_)) {
> if ($::saw_signal) {
> print "DEBUG: ignoring EOF\n";
> $::saw_signal = 0;
> next;
> } else {
> last; # true end-of-file
> }
> }
> # process input
> }
>
> ... except that it loses the line after the interrupt. Is there a better
> way to read standard input (sysread() blocks and break the blocks into
> lines?), or to catch interrupts, to avoid this?
Perhaps you could use the 4 argument form of select. Just stub off a HUP
handler and catch the select error return:
{
my($rout, $rin);
$rout = $rin = "";
vec($rin, 0 , 1) = 1;
local $SIG{HUP} = sub { };
while (1) {
$nfound = select ($rout=$rin, undef, undef, undef ) ) {
if ( $nfound == -1 ) { # read interrupted by signal
read_config();
next;
} else (
$nread = sysread (STDIN, $in, $nfound) ;
last if $nread == 0; # exited
};
}
}
HTH,
--
Charles DeRykus
ced@carios2.ca.boeing.com
------------------------------
Date: 11 Aug 1997 22:55:26 GMT
From: "TIM ARMBRUSTER" <armbrust@mcnet.mcpherson.edu>
Subject: Re: sleep for a few hundrad milli seconds
Message-Id: <01bca6a8$5dd9c280$a5a001cf@Services.midusa.net>
> On Mon, 11 Aug 1997, Syam P. Aribindi wrote:
>
> > I need to routine which allows a sleep time of less of 1 second.
>
This works, but I'm not sure what the numeric value (0.1) means. I think
it means 0.1 seconds, though. It works just like sleep, but in increments
of less than 1 second.
select(undef,undef,undef,0.1)
It's wierd, I know, but straight from my book.
------------------------------
Date: 11 Aug 1997 21:37:53 GMT
From: thomas_wernitz@tait.co.nz (Thomas Wernitz)
Subject: Re: Sorting this file is killing me
Message-Id: <5so0nh$qu1@gatekeeper.tait.co.nz>
Hi Tom,
I checked it out. It isn't that bad, though it's not working! ;)
Here is a working one:
chomp(@source = <DATA>); # slurp in the file
map {push @sorted, [split /\|/,$_]}
sort {(split(/\|/,$a))[1] <=> (split(/\|/,$b))[1]} @source;
grep {print "$_->[0] : $_->[1]"} @sorted;
__DATA__
foo|100.00
bart|1000.00
mag|500.00
This does the trick!!! I just forgot to split again after sorting, but that
was obvious, I hope. :-S
Hey Tom why is it so bad to use map in a void context. If it does what you
want, how can it be wrong?
Cheers,
Thomas
------------------------------
Date: 11 Aug 1997 21:48:43 GMT
From: tfletche@pcocd2.intel.com (Terry Michael Fletcher - PCD ~)
Subject: Re: splice
Message-Id: <5so1br$eel$1@news.fm.intel.com>
Jeroen Kustermans (kusterma@htsa.hva.nl) so eloquently and verbosely pontificated:
> I've got the following problem:
>
> If it's there, remove that entry from my array
>
> $counter=0;
> foreach (@aliasfile) {
> print "$_ : $counter\n";
> if (/test/) {
> splice(@aliasfile, $counter, 1);
> }
> $counter++;
> }
to quote from a similar question in the FAQ:
Q: "What happens if I add or remove keys from a hash while iterating
over it?
A: "Don't do that."
well, same thing goes for arrays.
the problem is that the foreach preindexes the elements of the list that
you want to iterate over. for example, you have a list of five elements.
you get to element 3 and wish to extract it, so you use splice. now your
array[4] has just shifted to array[3] and array[5] goes to array[4]. the
next iteration of the loop examines the new array[4] because it already
has examined array[3] ! you have just skipped the original 4th element,
no matter what your $counter value is.
i ran into this problem before, and mine is an ugly inefficient solution.
(restart the entire loop if an extraction was made, but in my case, the
list was small)
i was also lazy, and didnt try the range operator (which by the way would
do your counting for you...)
for $i (0..$#aliasfile) {
$_ = $aliasfile[$i];
print "$_ : $i\n";
if (/test/) {
splice(@aliasfile, $i, 1);
}
}
but almost positive that the $#aliasfile value is not re-evaluated each
time. what you could do (aside from restarting a new loop) is this
(really ugly):
for $i (0..$#aliasfile) {
$i || $j = 0;
$_ = $aliasfile[$i-$j];
print "$_ : ".$i-$j."\n";
if (/test/) {
splice(@aliasfile, $i-$j, 1) && $j++;
}
}
the best option would be to replace what you splice with a placeholder,
splice(@aliasfile, $i, 1, "");
but since you need access to the array again, you want those elements
*gone*.
anyways, sorry bout the sloppy code, but hopefully you could at least
understand why it is bad to extract from a list you are iterating over!
--
"Give me ambiguity or give me something else."
______ ______ _ _
(_) | (_) | | | | |
| _ ,_ ,_ _|_ | | _ _|_ __ | | _ ,_
_ ||/ / | / | | | / | ||/ |/ | / |/ \ |/ / |
(_/ |__/ |_/ |_/ \_/|/ (_/ |__/|__/|_/\___/| |_/|__/ |_/
*+*+*+*+*+*+*+*+*+*+*+* /| *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
\| tfletche@pcocd2.intel.com
*+*+*+*+*+ Views expressed...not INTeL's...yadda yadda yadda.... *+*+*+*+*+*
------------------------------
Date: Mon, 11 Aug 1997 14:35:20 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: "Mr. Blue" <car@access2.digex.net>
Subject: Re: Substring substitution in PATH variable
Message-Id: <Pine.GSO.3.96.970811143437.27045D-100000@julie.teleport.com>
On 11 Aug 1997, Mr. Blue wrote:
> We need a perl script that will substitute substrings within the
> PATH environment variable.
This FAQ entry may be useful. Hope this helps!
http://www.perl.com/CPAN/doc/FAQs/FAQ/html/perlfaq8/
I_changed_directory_modified_m.html
--
Tom Phoenix http://www.teleport.com/~rootbeer/
rootbeer@teleport.com PGP Skribu al mi per Esperanto!
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: 11 Aug 1997 23:48:26 GMT
From: ap958@chebucto.ns.ca (Jason Nugent)
Subject: Re: textarea - Script not producing line feeds??
Message-Id: <5so8ca$p26$1@News.Dal.Ca>
: On 4 Aug 1997 14:52:23 GMT, Burt lewis@ici.net (Burt Lewis) wrote:
: ()In my Perl scripts I have:
: ()
: ()print "<TR><TD><textarea Rows=8 Cols=65 name=\"body\"
: ()wrap=\"physical\"></textarea></TD></TR>\n";
: ()
: ()Problem is that I need line feeds after each line that is wrapping
: and
: ()I'm not getting any. The input is coming out as one long string
: with
: ()no breaks.
: ()
: ()Any ideas or suggestions is appreciated.
If your problem is that you are getting your info from the form in
one long line, you might want to try changing your textarea tag to
wrap=hard. I find that this works for me, and all the data I get from my
textarea fields comes through in multiple lines.
Hope this helps, Jason
---------------------
Jason Nugent, BSc(Hons)
ap958@chebucto.ns.ca "My strength is in numbers, and my soul
Webmaster lies in every one. The releasing of anger
Member, HTML Writer's Guild can better any medicine under the sun."
Live to ride, - Pantera, Mouth for War
Ride to Live.
********************************************************************
The Best Place for Sport on the net! - Sportscience www.sportsci.org
********************************************************************
------------------------------
Date: Tue, 12 Aug 1997 07:31:02 +0900
From: "Hyung-Tae, Kim" <htkim@wsn.co.kr>
Subject: the port of GD graphic library module for NT alpha ?
Message-Id: <33EF92A6.1FAD3211@wsn.co.kr>
is there any one who
has the port of GD graphic library module for NT alpha ?
i'm using Activeware's Perl5(build 307) on NT 4.0 DEC alphaserver.
i can find the module for x86 on "ftp://ftp.roth.net/pub/NTPerl/GD/".
but can't for DEC alpha chips.
if anyone has the port for NT alpha, please notify me.
bye.
------------------------------
Date: 11 Aug 1997 23:25:32 GMT
From: ebresie@ccwf.cc.utexas.edu (Eric Bresie)
Subject: url_get undefined subroutine
Message-Id: <5so71c$4ek$1@geraldo.cc.utexas.edu>
I am in thr process of working on some CGI scripts which require the
use of either url_get or w3get to retrieve some web pages for a script.
I got all the appropriate files (I hope) and when I execute
url_get http://mvrhelp
I get the following error:
Undefined subroutine &ftp::SYS_gethostname called at ftplib.pl line 36.
Line 36 of ftlib.pl has the following line:
(syscall(&SYS_gethostname, $Myhost, 65) == 0 ) ||
die "cannot 'gethostname4' of local machine (in ftplib)"
ftplib.pl contains the following requires:
require "chat2.ph"
require "sys/syscall.ph"
(I may be slightly off on the exact names but they are basically the
same as what is in ftplib.pl)
I am using perl 5.0.3 on a Solaris 2.4 machine.
Any help would be greatly appreciated.
============================================================================
* Eric Bresie *
* ebresie@ccwf.cc.utexas.edu *
* http://ccwf.cc.utexas.edu/~ebresie *
============================================================================
------------------------------
Date: 11 Aug 1997 20:14:36 GMT
From: bdwheele@indiana.edu (Brian Wheeler)
Subject: Re: Visual representation of UNIX Directories?
Message-Id: <5snrrc$3eg$1@dismay.ucs.indiana.edu>
In article <5snmju$d4c@sun004.atm.dsccc.com>,
ldjacobs@atm.dsccc.com (Lance D. Jacobs) writes:
>Marjorie Roswell (roswell@umbc.edu) wrote:
>
>[snip]
>
>: I'm looking for a visual representation of directories in UNIX. Any
>: solutions are welcome.
>
>
>If a quick-and-dirty, text-based representation is OK, try this:
>----------------------------------------------------------------
<< 165 line program snipped >>
That's pretty long for quick-and-dirty. While not nearly as pretty, here's
my try:
#!/usr/bin/perl
if($ARGV[0] eq "") {
print STDERR "Usage: $0 <directory>\n";
exit;
}
srand;
&ScanDir($ARGV[0]);
sub ScanDir {
my($dir,$depth)=@_;
my($dhandle)=time+rand; # get a random thing to use as a handle
my($file);
opendir($dhandle,$dir);
while(defined($file=readdir($dhandle))) {
next if($file eq ".");
next if($file eq "..");
print "| "x$depth;
if(-d "$dir/$file") {
print "+--$file\n";
&ScanDir("$dir/$file",$depth+1);
} else {
print " $file\n";
}
}
closedir($dhandle);
}
--
Brian Wheeler
bdwheele@indiana.edu
------------------------------
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 849
*************************************