[23214] in Perl-Users-Digest
Perl-Users Digest, Issue: 5435 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 4 03:06:03 2003
Date: Thu, 4 Sep 2003 00:05:10 -0700 (PDT)
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, 4 Sep 2003 Volume: 10 Number: 5435
Today's topics:
Re: [newbie] how to get just the first value of a hash? <jurgenex@hotmail.com>
Re: [newbie] how to get just the first value of a hash? <jurgenex@hotmail.com>
Re: [newbie] how to get just the first value of a hash? <tassilo.parseval@rwth-aachen.de>
Re: A suggestion for perldoc <ict@eh.org>
Re: FAQ creation (debraj)
Re: Getting directory sizes on win32 <jwillmore@cyberia.com>
Help with IO::socket simultaneous read and write (Steve D)
Re: learning examples <print split /!/"d!a!v!i!d!o!@!p!a!c!i!f!i!e!r!.!c!o!m!\n">
Re: Perl6 internals Archives? (trwww)
Redirect stdio into a string (Initial-N)
Re: Replacing a line of text in an HTML file ??? <NoSpamPlease@bellsouth.net>
Re: Silly push tricks (Sam Holden)
Re: Silly push tricks (Sam Holden)
Re: Silly push tricks <abigail@abigail.nl>
Re: Silly push tricks <cwilbur@mithril.chromatico.net>
Re: Silly push tricks <abigail@abigail.nl>
Re: Strange OIDs returned for IBM 3584 v2c trap variabl <NOSPAM.mikek-cpsnmp@muonics.com.NOSPAM>
Text File Processing (Greg Carlill)
Re: Text File Processing <REMOVEsdnCAPS@comcast.net>
Re: Text File Processing <krahnj@acm.org>
Re: The life of $! (James Willmore)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 04 Sep 2003 02:46:04 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: [newbie] how to get just the first value of a hash??
Message-Id: <MHx5b.12832$NF5.8398@nwrddc01.gnilink.net>
Guest1 wrote:
[see subject]
Hashes are mappings and by definition don't have an order, therefore there
is no first element or first value.
Or how would you select the first value of let's say the cubicroot function?
jue
------------------------------
Date: Thu, 04 Sep 2003 02:50:00 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: [newbie] how to get just the first value of a hash??
Message-Id: <sLx5b.12839$NF5.4793@nwrddc01.gnilink.net>
Brian Harnish wrote:
> Well, we're all entitled to oppinions. I don't think it's difficult to
> jump to the end of the message and delete a few lines when replying.
> And the only reason it got so ugly in our quoting is because we
> quoted the sigs, because thats what we were discussing.
If it would be a sig then probably nobody would complain because smart
Newsreaders will remove the sig automatically in replies.
Unfortunately it is not marked a sig, so it is body content, and will be
copied automatically, and requires manual effort to be removed.
Sucks IMHO
jue
------------------------------
Date: 4 Sep 2003 06:11:13 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: [newbie] how to get just the first value of a hash??
Message-Id: <bj6l21$5je$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Jürgen Exner:
> Guest1 wrote:
> [see subject]
>
> Hashes are mappings and by definition don't have an order, therefore there
> is no first element or first value.
> Or how would you select the first value of let's say the cubicroot function?
Well, the first one is probably 0 (at least for real numbers). The
challenge is to find the second value since it has a non-discrete
carrier.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: 4 Sep 2003 10:23:19 +1000
From: Iain Truskett <ict@eh.org>
Subject: Re: A suggestion for perldoc
Message-Id: <slrnbld1pi.vj1.ict@gytha.anu.edu.au>
* Michele Dondi <bik.mido@tiscalinet.it>:
[...]
> OTOH every now and again reading this ng or a doc/faq
> while looking for something else I happen to discover some
> very cool feature I was unaware of...
Perhaps purchase "Perl Cookbook" (2nd edition is freshly
released). It contains many useful tips, all in bite sized
morsels.
cheers,
--
Iain.
------------------------------
Date: 3 Sep 2003 22:50:48 -0700
From: debhatta@hotmail.com (debraj)
Subject: Re: FAQ creation
Message-Id: <f9f243e.0309032150.73372938@posting.google.com>
Hi All
Thanx for all those follow ups . Now I have a tidy FAQ ( FAQ-O-Matic)
downloaded from sourceforge site . I also tried one from freshmeat.com
called (FAQ Plain) but that didnt have all the features which I was
looking for .
Thanx again,
Debhatta
------------------------------
Date: Thu, 04 Sep 2003 04:24:44 GMT
From: James Willmore <jwillmore@cyberia.com>
Subject: Re: Getting directory sizes on win32
Message-Id: <20030904002439.6b75951e.jwillmore@cyberia.com>
On 3 Sep 2003 14:04:18 -0700
deaduser@hotmail.com (Jeffrey Ellin) wrote:
> Hi, I am using the following code to get the directory sizes of
> users outboxes on our appservers. This code snippet works but it is
> dreadfully slow. I have also used File:Find, but it doesn't seem any
> faster. Any ideas on how to speed it up? Everything is running on
> Win2K.
>
> #sql to get all active users and their last sync date, exclude users
> who
> #are enddated in the system
> $sql = " select n.name,n.APP_SERVER_NAME, max(s.LAST_UPD) as
> sync_date " .
> " from siebel.s_node n, " .
> " siebel.s_extdata_node e, " .
> " siebel.s_dock_status s " .
> " where n.ROW_ID = s.node_id and " .
> " e.NODE_ID = n.ROW_ID and " .
> " n.node_type_cd = 'REMOTE' and " .
> " s.type = 'SESSION' and " .
> " local_flg = 'N' and " .
> " e.ACTIVE_FLG = 'Y' and " .
> " (n.EFF_END_DT > sysdate or n.EFF_END_DT is null)" .
> " group by n.name, n.APP_SERVER_NAME " .
> " order by sync_date " ;
You could use a here doc for this part. Won't do wonders for speed,
but will aid in debugging later.
==untested==
$sql = <<SQL;
select n.name,n.APP_SERVER_NAME, max(s.LAST_UPD) as
sync_date
from siebel.s_node n,
siebel.s_extdata_node e,
siebel.s_dock_status s
where n.ROW_ID = s.node_id and
e.NODE_ID = n.ROW_ID and
n.node_type_cd = 'REMOTE' and
s.type = 'SESSION' and
local_flg = 'N' and .
e.ACTIVE_FLG = 'Y' and
(n.EFF_END_DT > sysdate or n.EFF_END_DT is null)
group by n.name, n.APP_SERVER_NAME
order by sync_date
SQL
++end++
>
> #execute sql
> $sth = $dbh->prepare($sql);
> $sth->execute;
>
> #delete old report file
> unlink 'outboxreport.csv';
I'm thinking that you may fair better if you store the results of the
query in a hash, _then_ iterate through the hash doing stuff with the
files/directories. That's just an opinion and it's unproven. My
thinking is that the longer you have the query open, the more
resources you're using. Of course, storing the information from the
query takes up resources as well. So, pick you poison.
> #loop through each user in resultset.
> while (($node,$server,$sync)=$sth->fetchrow_array()){
> #get name of docking directory
> my $dockloc = substr($server,6);
> #assemble path statement
> my $path = "//$server/docking$dockloc/$node/outbox";
> #my $path = "//$server/docking/$node/outbox";
> #get directory size
> my $dirsize = -s $path;
> opendir(my ($dh),$path);
>
> #loop through each file in the directory skip over dat and uaf
> since
> they are part of new database
> while( defined( my $filename = readdir $dh ) ) {
> next if $filename eq "." or $filename eq ".." or
> $filename
> =~ /uaf/ or $filename =~ /dat/;
> $dirsize += -s "$path/$filename";
> }
>
> #re-open file so it writes as we process
> open REP, ">>outboxreport.csv";
> #convert file size to megabytes
> $dirsize = $dirsize/1000000;
> #round file size
> $dirsize = sprintf "%.2f", $dirsize;
> #print out report in csv format
> print REP "$node,$server,$sync,$dirsize\n";
> }
When you say slow, how slow? And how much data are we talking about?
I mean, if your talking terrabytes, it's going to take some time to
get that information. Plus, consider the platform and how it handles
memory, resources, etc. More memory will mean some better
performance, etc.
Just my zero cents - money back if not satisfied :)
--
Jim
---
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
---
a real quote ...
Linus Torvalids: "They are somking crack ...."
(http://www.eweek.com/article2/0,3959,1227150,00.asp)
---
a fortune quote ...
"I know the answer! The answer lies within the heart of all
mankind! The answer is twelve? I think I'm in the wrong
building." -- Charles Schulz
------------------------------
Date: 3 Sep 2003 21:05:09 -0700
From: google.deller@smsail.com (Steve D)
Subject: Help with IO::socket simultaneous read and write
Message-Id: <e41b2b3b.0309032005.cf88ad8@posting.google.com>
I've created an application that opens sockets (using IO::Socket) and
then creates two ithreads, one that reads the socket and one that
writes it. It appears that if there is a "read" pending (sysread,
recv, or read) on a socket, then a "write" (syswrite, send or print)
on that socket hangs.
(In fact, it appears that if there is a "read" on *any* socket file
handle, then a "write" on any other hangs, but I need to verify that
carefully).
Before I put the reader's in, the writers were working just fine.
Is this known behavior with sockets?
How about with IO::Socket?
I'm trying to determine if there is a bug in my code, or if this is
known behavior.
Any good solutions?
Would IO::Select on the file-handle's in the reader's suffice to keep
the file-handle's clear for the writers to succeed?
Or will that fail too (if IO::Select somehow locks a file-handle).
Would Socket work better than IO::Socket without doing a select?
Regards,
Steve D
------------------------------
Date: Wed, 3 Sep 2003 23:06:20 -0700
From: "David Oswald" <print split /!/"d!a!v!i!d!o!@!p!a!c!i!f!i!e!r!.!c!o!m!\n">
Subject: Re: learning examples
Message-Id: <vldlmooi9ql2c4@corp.supernews.com>
"Sam" <samj@austarmetro.com.au> wrote in message
news:3f53c04d@news.comindico.com.au...
> the first is as bellow it prints a "\n" in the output right after the
$nmber
> so that converts to ... is on a new line. why?
>
> #!/usr/bin/perl -w
> use strict;
^^^^^ --- That's a good start.
> print "please type a hexadecimal number\n";
Stylistically it's better to put a space between print and what comes after
it. Not usually mandatory, but always a cleaner way of doing things.
> print "it should starts with 0x and ends with a character less than F: ";
What's wrong with that line is grammar, and the mistruth that tells people
the number should start with 0x. That's not necessary for use in the hex
function. Hex already knows it's getting a hexidecimal number. Also not
sure why your hexidecimal number needs to be less than F. F is a legitimate
number (15 in base 10).
> my $nmber= <STDIN>;
This is fine, but the next line needs to be:
chomp $nmber;
Chomp cuts off trailing newlines. When the user entered the data, he had to
hit enter to get the terminal to send it to your program, and in so doing, a
newline character was appended to what he typed. That's normal behavior,
and the primary justification for the existance of the chomp function.
> print "\$$nmber converts to decimal number: ", hex("$nmber"), "\n";
There is no need to quote $nmber inside of the hex function. The line
should read:
print "\$$nmber converts to decimal number:", hex ( $nmber ), "\n";
It wouldn't hurt to get the best start anyone could have by checking out at
the library or buying at your local technical bookstore a copy of Learning
Perl, published by O'Rilley & Assoc, written by Tom Christiansen and Randal
Schwartz. It is *the* definitive source for getting people started on
Perl.... just a thought.
Dave
------------------------------
Date: 3 Sep 2003 22:18:00 -0700
From: toddrw69@excite.com (trwww)
Subject: Re: Perl6 internals Archives?
Message-Id: <d81ecffa.0309032118.54bc5d8e@posting.google.com>
jgamble@ripco.com (John M. Gamble) wrote in message news:<bj5clt$gob$1@e250.ripco.com>...
> I used to drop in occasionally at the perl6-internals archive at
> http://archive.develooper.com/ to see how development was going.
>
> The entries seem to have vanished past 2002, and nothing new
> is posted. Has the archive moved? If so, where to?
You can find the archive of all newsgroups on nntp.perl.org at:
http://groups.google.com/groups?group=perl
the archive of perl.perl6.internals is at:
http://groups.google.com/groups?group=perl.perl6.internals
Todd W.
------------------------------
Date: 3 Sep 2003 23:08:16 -0700
From: neruson_newsletter@yahoo.ca (Initial-N)
Subject: Redirect stdio into a string
Message-Id: <368466cc.0309032208.4af16e6@posting.google.com>
Hi all,
How to catch all stdio output into a string?
I am using system() to call a external tool (exe), with some
arguments. Just doing so, it will print out all lot of data (which I
need to process it within the script). I wonder how I can catch all
the stdio ouput and redirect them into a string so that I can parse
it.
Thanks everyone!! I would like much appreciate if you can email at
neruson_newsletter@yahoo.ca
Intial-N
------------------------------
Date: Wed, 3 Sep 2003 19:13:29 -0400
From: "Rodney" <NoSpamPlease@bellsouth.net>
Subject: Re: Replacing a line of text in an HTML file ???
Message-Id: <kzu5b.48$fF.5@bignews4.bellsouth.net>
Thanks John... the next; function did the trick.
:)
--
...
`·.¸¸.·´¯`·.¸¸.·´¯`·-> rodney
------------------------------
Date: 3 Sep 2003 21:20:31 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: Silly push tricks
Message-Id: <slrnblcmov.lqg.sholden@flexal.cs.usyd.edu.au>
On Wed, 03 Sep 2003 21:15:04 GMT,
Charlton Wilbur <cwilbur@mithril.chromatico.net> wrote:
>>>>>> "A" == Abigail <abigail@abigail.nl> writes:
>
> [using map in void context]
>
> A> Poor style? Why?
>
> Because
>
> map { fn($_) } @list;
>
> and
> fn ($_) foreach @list;
>
> (where fn is a function with side-effects) do the same thing, but the
> latter is better style.
Well, they aren't exactly same. The first calls fn() in a list context,
while the second calls it in a scalar context...
--
Sam Holden
------------------------------
Date: 3 Sep 2003 21:23:49 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: Silly push tricks
Message-Id: <slrnblcmv5.lsp.sholden@flexal.cs.usyd.edu.au>
On 3 Sep 2003 21:20:31 GMT, Sam Holden <sholden@flexal.cs.usyd.edu.au> wrote:
>
> Well, they aren't exactly same. The first calls fn() in a list context,
> while the second calls it in a scalar context...
s/scalar/void/
--
Sam Holden
------------------------------
Date: 03 Sep 2003 22:49:42 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Silly push tricks
Message-Id: <slrnblcs06.n3b.abigail@alexandra.abigail.nl>
Charlton Wilbur (cwilbur@mithril.chromatico.net) wrote on MMMDCLV
September MCMXCIII in <URL:news:878yp57f6r.fsf@mithril.chromatico.net>:
:) >>>>> "A" == Abigail <abigail@abigail.nl> writes:
:)
:) [using map in void context]
:)
:) A> Poor style? Why?
:)
:) Because
:)
:) map { fn($_) } @list;
:)
:) and
:) fn ($_) foreach @list;
:)
:) (where fn is a function with side-effects) do the same thing, but the
:) latter is better style.
Beside that they are different (the context in which fn is called
differs) you don't give *any* argument why one is better style
than the other.
:) The issue that map can be inefficient when called in void context is
:) orthogonal to the style issue.
But that's the usual argument against using map in void context.
Abigail
--
package Just_another_Perl_Hacker; sub print {($_=$_[0])=~ s/_/ /g;
print } sub __PACKAGE__ { &
print ( __PACKAGE__)} &
__PACKAGE__
( )
------------------------------
Date: Thu, 04 Sep 2003 00:15:07 GMT
From: Charlton Wilbur <cwilbur@mithril.chromatico.net>
Subject: Re: Silly push tricks
Message-Id: <87znhl5sfw.fsf@mithril.chromatico.net>
>>>>> "A" == Abigail <abigail@abigail.nl> writes:
A> Beside that they are different (the context in which fn is
A> called differs) you don't give *any* argument why one is better
A> style than the other.
map means "generate a new list by applying this block to each element
of this other list." foreach means "execute this block once for each
element of the list." When you use them interchangeably, you confuse
that meaning, even though the code does (largely) the same thing.
I mean, if we saw this code:
my $i = 1;
while ($i <= 30)
{
fn ($i);
$i++;
}
we'd consider it bad style. It does the same thing as
for (my $i=1; $i <= 30; $i++)
{
fn ($i);
}
or
fn ($_) foreach (1..30);
but the clarity of them varies. This is why we say "it's bad style"
and not "it's incorrect code."
As a parallel example, I also use 'for' for C-style for (;;) loops and
'foreach' for iterating over a list with a variable; I'd say swapping
those two, even though they're synonymous to the interpreter, is also
bad style.
A> But that's the usual argument against using map in void
A> context.
It's certainly not *my* usual argument.
Charlton
--
cwilbur at chromatico dot net
cwilbur at mac dot com
------------------------------
Date: 04 Sep 2003 06:42:49 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Silly push tricks
Message-Id: <slrnbldnn9.bhv.abigail@alexandra.abigail.nl>
Charlton Wilbur (cwilbur@mithril.chromatico.net) wrote on MMMDCLVI
September MCMXCIII in <URL:news:87znhl5sfw.fsf@mithril.chromatico.net>:
\\ >>>>> "A" == Abigail <abigail@abigail.nl> writes:
\\
\\ A> Beside that they are different (the context in which fn is
\\ A> called differs) you don't give *any* argument why one is better
\\ A> style than the other.
\\
\\ map means "generate a new list by applying this block to each element
\\ of this other list."
Considering that there are no lists in scalar context, map cannot mean
that. Map means "apply this block on each element of the list". Map in
list context returns a list of the results - map in scalar context returns
a scalar, not a list.
\\ foreach means "execute this block once for each
\\ element of the list." When you use them interchangeably, you confuse
\\ that meaning, even though the code does (largely) the same thing.
\\
\\ I mean, if we saw this code:
\\
\\ my $i = 1;
\\ while ($i <= 30)
\\ {
\\ fn ($i);
\\ $i++;
\\ }
\\
\\ we'd consider it bad style. It does the same thing as
\\
\\ for (my $i=1; $i <= 30; $i++)
\\ {
\\ fn ($i);
\\ }
\\
\\ or
\\
\\ fn ($_) foreach (1..30);
\\
\\ but the clarity of them varies. This is why we say "it's bad style"
\\ and not "it's incorrect code."
So, the "there is more than one way of doing it" is just a marketing
slogan, because in reality, there's just one way of doing it, and
alternatives are "bad style"?
\\ As a parallel example, I also use 'for' for C-style for (;;) loops and
\\ 'foreach' for iterating over a list with a variable; I'd say swapping
\\ those two, even though they're synonymous to the interpreter, is also
\\ bad style.
In perl6, there won't be a 'foreach' keyword. Oh my! You will be
programming in bad style!
Abigail
--
perl -swleprint -- -_='Just another Perl Hacker'
------------------------------
Date: Thu, 04 Sep 2003 05:35:07 GMT
From: Michael Kirkham <NOSPAM.mikek-cpsnmp@muonics.com.NOSPAM>
Subject: Re: Strange OIDs returned for IBM 3584 v2c trap variables
Message-Id: <MPG.19c06cfbdd33230e989748@news.sf.sbcglobal.net>
In article <d27434e.0309030628.55ae32d4@posting.google.com>,
john_ramsden@sagitta-ps.com says...
> But in traps received from an IBM 3584, the trap variables' OIDs
> end with '.0.0' (except, in v2c traps, the first two standard
> variables namely sysUpTime.0 and snmpTrapOID.0) when these
> should end with '.1.0'.
>
> When my code replaces the '.0.0' strings by '.1.0' (and then strips
I don't see why you'd do that. If you do that in general you're liable
to wind up with something very different from what's actually being
represented by the trap's OIDs.
> But I'm wondering if this is some peculiarity or bug specific to
> the IBM MIB or yet another weird quirk of SNMP itself. Does an OID
> with more than one trailing zero have any special significance,
> or is there a standard way of dealing with it?
The .0.0 appears to be a bug in the agent, based on your decoder trace
and the MIB excerpt:
> ibm3584MIBObjectsMTMNLSN OBJECT-TYPE
> SYNTAX DisplayString (SIZE (1..16))
> MAX-ACCESS read-only
> STATUS current
> DESCRIPTION "This is the machine type associated with the trap."
> ::= { ibm3584MIBGroupMTMNLSN 1 }
...
> "name" => "ibm3584MIBObjectsMTMNLSN",
...
> "oid" => "1.3.6.1.4.1.2.6.182.1.2.11.1",
...
> 0049 06 14: OBJECT ID
> = 1.3.6.1.4.1.2.6.182.1.2.11.0.0
> 0059 04 16: STRING
> = '3584 L32 1311031'
1.3.6.1.4.1.2.6.182.1.2.11.1 <-- OBJECT-TYPE
1.3.6.1.4.1.2.6.182.1.2.11.0.0 <-- trap
Maybe this is why you are being inclined to replace .0.0 by .1.0, but
something else is wrong here. If that actually *is* intended to be
ibm3584MIBObjectsMTMNLSN, then the OID in the trap wrong (or the MIB
definition is wrong). According to the MIB definition it should be:
1.3.6.1.4.1.2.6.182.1.2.11.1.0
Unless what you're actually seeing is some *other* object, defined as..
::= { ibm3584MIBGroupMTMNLSN 0 }
This might be because you're actually seeing some other notification from
the one you quoted from the MIB, since you didn't provide the full
NOTIFICATION-TYPE definition there's no way for me to match up the OID in
the notification to the NOTIFICATION-TYPE.
--
Michael Kirkham
Muonics
http://www.muonics.com/
------------------------------
Date: 3 Sep 2003 19:27:39 -0700
From: gregcarlill@energex.com.au (Greg Carlill)
Subject: Text File Processing
Message-Id: <7a78a207.0309031827.76b3f46d@posting.google.com>
Hi All,
I have a text file that contains column data like:
730 B13
730 B33
730 B53
730 B73
800 B10-1
800 B30-1
800 B50-1
800 B70-1
and want to get a output text file like this.
730 B13, B33, B53, B73
800 B10-1, B30-1, B50-1, B70-1
Perl 5.005_02 on NT is all I have to do this. What is the best way to
attack this. I have almost no knowledge of Perl but am willing to
learn what I need.
Thanks in advance
Greg
------------------------------
Date: Wed, 03 Sep 2003 21:57:15 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Text File Processing
Message-Id: <Xns93EBE9A846943sdn.comcast@206.127.4.25>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
gregcarlill@energex.com.au (Greg Carlill) wrote in
news:7a78a207.0309031827.76b3f46d@posting.google.com:
> Hi All,
>
> I have a text file that contains column data like:
>
> 730 B13
> 730 B33
> 730 B53
> 730 B73
> 800 B10-1
> 800 B30-1
> 800 B50-1
> 800 B70-1
>
> and want to get a output text file like this.
>
> 730 B13, B33, B53, B73
> 800 B10-1, B30-1, B50-1, B70-1
>
> Perl 5.005_02 on NT is all I have to do this. What is the best way to
> attack this. I have almost no knowledge of Perl but am willing to
> learn what I need.
First of all, a couple questions: Are the values in the second column
going to be unique for each value in the right column? Do you need to
output the result in the same order as the values arrived in the first
column?
Assuming the answers are both "yes", I'd attack the problem as follows:
1. loop over each line in the file
a. split into two parts, $col1 and $col2.
b. push $col1 onto @col1values if $col2values{$col1} doesn't
exist.
c. push $col2 onto @{ $col2values{$col1} }.
2. loop over each value in @col1values
a. set $aref = $col2values{$_}
b. print $_, @$aref (suitably formatted).
In other words, keep the column 1 values in an array (to preserve order),
and keep the column 2 values in a hash-of-arrays, keyed on the column 1
value.
If the output order isn't significant, you can dispose of the col1values
array -- just use the keys() of %col2values.
If the second column values are not necessarily unique, but you want to
remove dups for the output, you'll have to modify %col2values to be a
hash-of-hashes, and output keys(%{$col2values{$_}} instead of @$aref in
step 2b. If you additionally need to preserve input order, you'll have
to keep a parallel array of col2 values, like you did in @col1values.
Make sense? If not, feel free to ask me to elaborate :-)
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBP1aqP2PeouIeTNHoEQJTOgCZAcCZryK+/dFk7l9jJvaEnnCQHroAoL9L
p4GdE9QjeVP1xab/Qqe/cj0y
=kcCw
-----END PGP SIGNATURE-----
------------------------------
Date: Thu, 04 Sep 2003 05:10:48 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Text File Processing
Message-Id: <3F56C940.F453C85F@acm.org>
Greg Carlill wrote:
>
> I have a text file that contains column data like:
>
> 730 B13
> 730 B33
> 730 B53
> 730 B73
> 800 B10-1
> 800 B30-1
> 800 B50-1
> 800 B70-1
>
> and want to get a output text file like this.
>
> 730 B13, B33, B53, B73
> 800 B10-1, B30-1, B50-1, B70-1
>
> Perl 5.005_02 on NT is all I have to do this. What is the best way to
> attack this. I have almost no knowledge of Perl but am willing to
> learn what I need.
Here is one way to do it:
#!/usr/bin/perl -w
use strict;
my %data;
while ( <> ) {
my ( $key, $val ) = split or next;
if ( exists $data{ $key } ) {
$data{ $key } .= ", $val";
}
else {
print join( "\t", %data ), "\n" if %data;
%data = ( $key, $val );
}
}
print join( "\t", %data ), "\n" if %data;
__END__
John
--
use Perl;
program
fulfillment
------------------------------
Date: 3 Sep 2003 15:38:38 -0700
From: jwillmore@cyberia.com (James Willmore)
Subject: Re: The life of $!
Message-Id: <e0160815.0309031438.4d1d42cc@posting.google.com>
"Richard S Beckett" <spikey-wan@bigfoot.com> wrote in message news:<bii3i0$8mg$1@newshost.mot.com>...
> Guys,
>
> How long does $! live for after the event, as it were?
Short answer: until the next event that populates '$!'.
Long answer: '$!' holds the OS error. So, until an OS error occurs,
this is undef. When an OS error occurs (ex file not found), it is
populated with the error (FYI: there's a 'hack' to change the
"English" error into a numeric equivlent - $!+0 - ex.
open(F,"duh.txt") or die $!+0,"\n";). Unless you do something with
it, it will be replaced the next time there is an OS error.
That's my limited understanding of the whole thing :)
>
> For example, in the code snippet below, do I need to assign $! to
> $error_text on the second line, or can I use $! on the 8th line, and still
> get the correct error?
>
> Thanks.
>
> R.
>
> copy ("$source", "$dest") or do {
>
> my $error_text = $!; # -----> Do I really need to do this?
>
> my $error_window = $main_window -> DialogBox (
> -title => "$title - Error!",
> -buttons=>["OK"]);
> my $text = $error_window -> Label (-text=>
> "\nERROR: Cannot backup file!\n\n".
>
> "$error_text\n", # ------> Could I use $! here?
>
> )
> ->pack;
> $error_window -> bell;
> $error_window->Show();
> }
Ugh! Use Tk::ErrorDialog and Tk::Error instead of 'rolling your own'
error dialog. It handles errors, IMHO, much better than I ever could.
For example:
##untested##
#!/usr/bin/perl -w
use strict;
use Tk;
require Tk::ErrorDialog;
my $mw = new MainWindow();
#generate an error by calling to subroutine
open_file();
MainLoop;
sub open_file{
#notice the die statement
#we have 2 flavors: a message going to STDOUT and an error
#dialog being displayed
open(FILE,"duh.txt")
or die "File error: $!",$mw->Tk::Error("Can't open file\n$!\n");
my @file = <FILE>;
close FILE;
}
HTH
Jim
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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.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.
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 V10 Issue 5435
***************************************