[10491] in Perl-Users-Digest
Perl-Users Digest, Issue: 4083 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 27 15:05:49 1998
Date: Tue, 27 Oct 98 12:00:23 -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 Tue, 27 Oct 1998 Volume: 8 Number: 4083
Today's topics:
Re: Camel book: References. <d-edwards@nospam.uchicago.edu>
Re: Date 2 Num (Joergen W. Lang)
File locations <killer@ath.forthnet.gr>
Re: Forcing perl to garbage collect (Ilya Zakharevich)
Re: Forcing perl to garbage collect (Tye McQueen)
Re: Forcing perl to garbage collect (Ilya Zakharevich)
Re: looking for a perl interpreter for NT <ray@nospam.com>
Re: LWP::UserAgent, how to get headers? <jdporter@min.net>
Re: LWP::UserAgent, how to get headers? <jdf@pobox.com>
Re: LWP::UserAgent, how to get headers? (Clinton Pierce)
newbie needs help with file comparison loop 2 gsechen2@my-dejanews.com
Re: newbie needs help with file comparison loop 2 (Brand Hilton)
newbie needs help with file comparison loop gsechen2@my-dejanews.com
Re: newbie needs help with file comparison loop <jdporter@min.net>
Re: newbie needs help with file comparison loop (Larry Rosler)
Re: newbie needs help with file comparison loop <arnej@fc.hp.com>
Re: Not to start a language war but.. (Ilya Zakharevich)
Re: Not to start a language war but.. (David Formosa)
Re: Not to start a language war but.. <johnc@interactive.ibm.com>
Re: Perl & Y2K - booby trap code (Peter Scott)
Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 27 Oct 1998 18:08:39 GMT
From: Darrin Edwards <d-edwards@nospam.uchicago.edu>
Subject: Re: Camel book: References.
Message-Id: <tg1zntop20.fsf@noise.bsd.uchicago.edu>
kpreid@ibm.net (Kevin Reid) writes:
> Darrin Edwards <d-edwards@nospam.uchicago.edu> wrote:
>
> > If you give me, in C, a pointer to one of your precious data
> > structures -- something nice and OO, with lots of intricate
> > internal parts you'd rather not have us greasy-fingered users
> > touch -- guess what, (char *)p points to the first byte, (char *)p+1
> > to the second, I can stomp right through it byte by byte!
> >
> > If I give you, in perl, a ref to one of my hacked-up data
> > structures, yadda yadda -- guess what, perl doesn't allow
> > pointer arithmetic.
>
> Actually, Perl does something better - it lets you look at the data
> structure and find out what sort of things the elements are, allowing
> you to easily figure out the structure.
Yes, I mentioned the ref function elsewhere in that same post. Perhaps
I didn't make it clear enough that I think (rather, agree with
much smarter perl people who think that) forbidding pointer arithmetic
is generally a good thing... out of context, the (mild) sarcasm in "doesn't
allow" is perhaps not evident. I posted in response to the question:
>"misc.word.corp" <misc.word.corp@pobox.com> writes:
>> References in Perl seem a bit ornery, at least as presented in Camel.
>> I'm sure that they offer many advantages over their complements in C.
>> What exactly are those advantages, anyway?
however, so I thought at the time my intent was clear. (_I_ don't
think Perl reference syntax is ornery! :)
Darrin
------------------------------
Date: Tue, 27 Oct 1998 20:16:39 +0100
From: jwl@_munged_worldmusic.de (Joergen W. Lang)
Subject: Re: Date 2 Num
Message-Id: <1dhkj3m.xcsdz0z78uzwN@host050-210.seicom.net>
Craig Pickles <pickles_c@hotmail.com> wrote:
> Does anybody know of a place which has an archive of Perl
> SUBROUTINES...
>
> I am looking for a subroutine that would convert a date (in a specific
> format if needed) to a number and back again......
>
> I know the Date:Calc module would do this, but unofrtunately, I am
> running my site from a cheap server that doesnt allow me access to C
> for compiling......
>
> So all I want to know is this... is there a place on the web where I
> can find such things as subroutines, or do I have to create them
> myself...
>
> Thanking you in advance
> Craig
CPAN to the rescue. The Comprehensive Perl Archive Network has about all
you need.
http://www.perl.com/CPAN
BTW, you don't have to access C for compiling. Even if your provider
won't give you access to the modules or did not install, you can always
put them in a directory you have access to and put the line:
BEGIN { unshift(@INC, "path/to/your/modules") }
you can access the modules by putting:
use Date::Calc;
and it should be working.
If none of the above works, consider a new provider :-)
hth, HANW,
Joergen
--
To reply by email please remove _munged_ from address Thanks !
-------------------------------------------------------------------
"Everything is possible - even sometimes the impossible"
HOELDERLIN EXPRESS - "Touch the void"
------------------------------
Date: Tue, 27 Oct 1998 21:47:20 +0200
From: Basile Laderchi <killer@ath.forthnet.gr>
Subject: File locations
Message-Id: <36362348.592F78DA@ath.forthnet.gr>
i am trying' to built a perl script that accesses a file but the script
insists on needing' the file in the C:\
any ideas on how to convince the .pl or the server to make it look for
the file in the cgi-bin directory or in the webserver root?
i am using ActivePerl 505 with Alibaba server in Win95
Thanx in advance
--
/------------------------\
| Killer Angel |
+------------------------+
| Killer@ath.forthnet.gr |
| ICQ 2870180 |
+------------------------+
| Alpha World |
| 32746.8S 99.3E 270 |
\------------------------/
------------------------------
Date: 27 Oct 1998 18:20:07 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: Forcing perl to garbage collect
Message-Id: <7152sn$tl$1@mathserv.mps.ohio-state.edu>
[A complimentary Cc of this posting was sent to
<droby@copyright.com>],
who wrote in article <714l4c$f5o$1@nnrp1.dejanews.com>:
> > Out of curiosity, what happens if you add:
> >
> > $i =~ s/./\0/g;
> I was curious too, so I tried it. No go. In fact, it shows up twice with or
> without the clearing attempt. If you substitute $i = 'abc123' for $i = `cat
> z.in`, it reduces to one, so I guess one of them is a temporary copy created
> by the ``.
Correct. Temporaries are not touched (until reused, or interpreter
dies). But even if reused or forgotten, the string buffer is just
free()ed or realloc()ed, which does not guarantie anything.
> It might be that the s/./\0/g clears it but creates another
> temporary copy in the process.
No, it should not. An optimization should be triggered, and no new
memory is going to be allocated.
Ilya
------------------------------
Date: 27 Oct 1998 13:27:57 -0600
From: tye@fohnix.metronet.com (Tye McQueen)
Subject: Re: Forcing perl to garbage collect
Message-Id: <7156rt$q6r@fohnix.metronet.com>
gebis@fee.ecn.purdue.edu (Michael J Gebis) writes:
)
) I also think that 1) you can't force perl to garbage collect without
) exiting and 2) even if you could, there's no guarantee that garbage
) collection will clear memory, so garbage collection isn't what you
) want anyway. I'm sure someone more intimate with the guts of perl
) will speak up and correct me if I'm wrong.
1) Perl doesn't garbage collect.
2) What perl does do doesn't change much if any of the data found
in the buffers being freed for future use.
On (1), Perl frees most buffers once there are no more references
to them, with the closest thing to garbage collection being the
ability to slightly delay the decrementing of a refernce count
for temporary values that sit on Perl's stack. And this "garbage
collection" [called FREETMPS] can usually be triggered by
executing just one more Perl statement.
One reason I say "most" above is that buffers for lexical
variables [see C<my>] are not freed when they go out of scope
in hopes that we'll be back in scope soon and will regret
having to spend the time freeing and reallocating all those
buffers [which seems to annoy Ilya, understandably, since it
increases memory usage, sometimes greatly].
--
Tye McQueen Nothing is obvious unless you are overlooking something
http://www.metronet.com/~tye/ (scripts, links, nothing fancy)
------------------------------
Date: 27 Oct 1998 19:50:58 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: Forcing perl to garbage collect
Message-Id: <715872$639$1@mathserv.mps.ohio-state.edu>
[A complimentary Cc of this posting was sent to Tye McQueen
<tye@fohnix.metronet.com>],
who wrote in article <7156rt$q6r@fohnix.metronet.com>:
> One reason I say "most" above is that buffers for lexical
> variables [see C<my>] are not freed when they go out of scope
> in hopes that we'll be back in scope soon and will regret
> having to spend the time freeing and reallocating all those
> buffers
There are other things which are *implemented* almost the same way as
lexicals, and suffer the same problem. These are "pad-temporaries",
used, say, in
$x = $a * $b + $c;
which is in fact executed similar to
my $tmp1 ==== $a * $b;
my $tmp2 ==== $tmp1 + $c;
$x = $tmp2;
(here ==== denotes imaginary "assign without temporaries" operation).
As far as tempories contain numbers, they do not hog memory much (at
least according to Sarathy's experiments), but if they contain LONG
strings, the fact that they are never free()ed also leads to
accumulation of memory which is kept by Perl "just because".
Ilya
------------------------------
Date: Tue, 27 Oct 1998 14:32:11 -0500
From: Ray Smith <ray@nospam.com>
To: Chris Lee <lammie_pie@hotmail.com>
Subject: Re: looking for a perl interpreter for NT
Message-Id: <36361FBB.9768F5E7@nospam.com>
Chris Lee wrote:
> I need I perl interpreter for NT
> does anyone one know where I can down load this??
>
> chris
You need Active Perl. Get it free at:
http://www.ActiveState.com/ActivePerl/
-Ray
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Raymond Smith, Sales Information Systems Mgr.
Media Cybernetics, L.P. "The Imaging Experts"
8484 Georgia Avenue,
Silver Spring, MD 20910 U.S.A.
Tel: +301-495-3305 x249 Fax: +301-495-5964
Email: ray @ mediacy . com
http://www.mediacy.com
------------------------------
Date: Tue, 27 Oct 1998 13:46:30 -0500
From: John Porter <jdporter@min.net>
Subject: Re: LWP::UserAgent, how to get headers?
Message-Id: <36361506.CA18611B@min.net>
Clinton Pierce wrote:
>
> I've checked LWP::UserAgent, the pod, the FAQ and done a DejaNews search,
> and didn't find this answer. It's either a stupid question, or I'm just
> thinking about it the wrong way.
>
> If I've made a request for a page using LWP::UserAgent, how can I
> get the _headers_ of the received information. Code snippet follows:
>
> What I'm interested in is the "Content-type" returned, which describes
> the $res->content. Where can I get that? Is it part of the $res
> object, or part of the $request object?
Um, perldoc LWP::UserAgent clearly states that the
request() method returns an object of type HTTP::Response.
And perldoc HTTP::Response clearly states that it inherits from
HTTP::Message, and that among the inherited methods frequently used
are headers() and content(), and that headers() returns a ref to the
HTTP::Headers object contained by the Message.
More interestingly, methods invoked on the Message which are not
recognized as Message methods get passed on the the Headers object;
so that you can simply do this:
$content_type = $response->header( 'Content-type' );
--
John "Throbblefoot" Porter
Please Don't "Courtesy CC" me.
I read this newsgroup fanatically. You know that!
("Emailed only" is fine, though.)
"The people at the Grey Hotel
Are either aged or unwell." -- EG
------------------------------
Date: 27 Oct 1998 19:37:23 +0100
From: Jonathan Feinberg <jdf@pobox.com>
To: cpierce1@cp500.fsic.ford.com (Clinton Pierce)
Subject: Re: LWP::UserAgent, how to get headers?
Message-Id: <m3k91lhmvw.fsf@joshua.panix.com>
cpierce1@cp500.fsic.ford.com (Clinton Pierce) writes:
> If I've made a request for a page using LWP::UserAgent, how can I
> get the _headers_ of the received information. Code snippet follows:
LWP comes with a program, lwp-request, which has an option (-e) to
print out the headers. For example:
$ lwp-request -e http://www.yahoo.com/ | grep -i content-type
Content-Type: text/html
$
Let's see how they do it there...
$response = $ua->request($request);
[snip]
if ($opt_e) {
# Display headers
print $response->headers_as_string;
print "\n"; # separate headers and content
}
There you go.
--
Jonathan Feinberg jdf@pobox.com Sunny Brooklyn, NY
http://pobox.com/~jdf
------------------------------
Date: 27 Oct 1998 19:03:29 GMT
From: cpierce1@cp500.fsic.ford.com (Clinton Pierce)
Subject: Re: LWP::UserAgent, how to get headers?
Message-Id: <7155e1$drj1@eccws1.dearborn.ford.com>
In article <36361506.CA18611B@min.net>,
John Porter <jdporter@min.net> writes:
>Clinton Pierce wrote:
>>
>> I've checked LWP::UserAgent, the pod, the FAQ and done a DejaNews search,
>> and didn't find this answer. It's either a stupid question, or I'm just
>> thinking about it the wrong way.
>>
>> If I've made a request for a page using LWP::UserAgent, how can I
>> get the _headers_ of the received information. Code snippet follows:
>>
>> What I'm interested in is the "Content-type" returned, which describes
>> the $res->content. Where can I get that? Is it part of the $res
>> object, or part of the $request object?
>
>Um, perldoc LWP::UserAgent clearly states that the
>request() method returns an object of type HTTP::Response.
>
>And perldoc HTTP::Response clearly states that it inherits from
>HTTP::Message, and that among the inherited methods frequently used
>are headers() and content(), and that headers() returns a ref to the
>HTTP::Headers object contained by the Message.
So I can clearly not choose the wine in front of you.
I was under the dilusion that LWP could simply not borrow methods
from HTTP unless it was a subclass of HTTP (which it didn't appear
to be). Apparently, perl's objects aren't that "strict". Too much
java this week, I guess.
Thank you (especially others), my thinking has been corrected.
--
+------------------------------------------------------------------------+
| Clinton A. Pierce | "If you rush a Miracle Man, | http://www. |
| | you get rotten miracles" | dcicorp.com/ |
| fubar@ameritech.net |--Miracle Max, The Princess Bride| ~clintp |
+------------------------------------------------------------------------+
GCSd-s+:+a-C++UALIS++++P+++L++E---t++X+b+++DI++++G++e+>++h----r+++y+++>y*
------------------------------
Date: Tue, 27 Oct 1998 17:58:27 GMT
From: gsechen2@my-dejanews.com
Subject: newbie needs help with file comparison loop 2
Message-Id: <7151k3$2g3$1@nnrp1.dejanews.com>
Hello,
I'm very new to perl (and programming) yet I have been assigned to write a
program.
I have two data files both datafiles need to be read in. The first datafile
needs to be compared (line-by-line) to the second datafile. If a line of data
in the first datafile matches any line in the second data file I need it to
print the line and the line number.
Below is what I have come up with so far. I seem to have the most trouble
with the nested while loops that Im trying to use to do the line-by-line
comparison.
##test1.pl###############################
$infile1 = '/adm/gsechen/rick/sta/03303280_rda';
open (IN1, "$infile1") || die "Cannot create $infile1: $!";
$infile2 = '/adm/gsechen/rick/anomaly.rdb';
open (IN2, "$infile2") || die "Cannot create $infile2: $!";
$counter = 1;
while ($line_1 = <IN1>)
{
$counter++;
while ($line_2 = <IN2>)
{
if $line_1 = $line_2
{
print $line_1;
print $counter;
}
}
}
## anomaly.rdb'#################################
#the following is/should be tab delimited
staid date time medim stype sampl pcode source issue report-
date old-remark old-value
15S 10D 7N 1S 1S 8S 6S 5S 1S 10D 1S 8N<prec>3
03303280 1997.11.24 1500 9 9 99800009 00095 sas 1 1998.12.24
536.000
03303280 1997.11.24 1500 9 9 99800009 00930 sas 1 1998.12.24
35.434
03303280 1997.11.24 1500 9 9 99800009 00945 sas 1 1998.12.24
106.580
03303280 1997.11.24 1500 9 9 99800009 01130 sas 1 1998.12.24
11.128
03303280 1997.11.24 1500 9 9 99800009 90095 sas 1 1998.12.24
517.000
03303280 1997.11.24 1500 9 9 99800009 01020 sas 1 1998.12.24
52.000
03303280 1997.11.24 1500 9 9 99800009 01060 sas 1 1998.12.24
4.950
03303280 1997.11.24 1510 R 9 99800018 00095 sas 1 1998.12.24
536.000
03303280 1997.12.22 1300 9 9 99800010 01060 sas 1 1998.12.24
2.380
03303280 1997.12.22 1300 9 9 99800010 01090 sas 1 1998.12.24
25.750
03303280 1997.12.22 1308 Q 2 99800005 99104 sas 6 1998.12.24
03303280 1997.11.24 1500 9 9 99800009 S2001 sas 7 1998.12.24
03378500 1997.11.04 1130 9 9 99800001 01020 sas 1 1998.12.24
153.890
03378500 1997.11.04 1130 9 9 99800001 01060 sas 1 1998.12.24
7.700
03378500 1997.11.04 1130 9 9 99800001 01106 sas 1 1998.12.24
1.510
03378500 1997.12.16 1150 9 9 99800003 01056 sas 1 1998.12.24
8.540
03378500 1997.12.16 1150 9 9 99800003 01090 sas 1 1998.12.24
8.380
03609750 1997.11.05 1215 9 9 99800002 00608 sas 1 1998.12.24
0.172
03609750 1997.12.17 1230 9 7 99800004 01090 sas 4 1998.12.24
1.140
03609750 1997.12.17 1240 R 7 99800008 01090 sas 4 1998.12.24
12.050
03612500 1997.11.06 1140 9 9 99800005 00608 sas 1 1998.12.24
0.159
03612500 1997.11.06 1140 9 9 99800005 01030 sas 1 1998.12.24
1.460
03612500 1997.11.06 1140 9 9 99800005 01060 sas 1 1998.12.24
2.410
03612500 1997.11.06 1140 9 9 99800005 01090 sas 1 1998.12.24
2.680
03612500 1997.11.06 1140 9 9 99800005 01106 sas 1 1998.12.24
3.220
03612500 1997.12.18 1230 9 9 99800006 01060 sas 1 1998.12.24
2.560
03612500 1997.12.18 1230 9 9 99800006 01090 sas 1 1998.12.24
4.380
03612500 1997.12.18 1230 9 9 99800006 01106 sas 1 1998.12.24
3.240
03612500 1997.12.18 1238 Q 2 99800007 99104 sas 6 1998.12.24
##03303280_rda#############################################
#the following is/should be tab delimited
staid date time medim stype sampl pcode source issue report-
date old-remark old-value
15S 10D 7N 1S 1S 8S 6S 5S 1S 10D 1S 8N<prec>3
03303280 1997.11.24 1500 9 9 99800009 00095 sas 1 1998.12.24
536.000
03303280 1997.11.24 1500 9 9 99800009 00930 sas 1 1998.12.24
35.434
03303280 1997.11.24 1500 9 9 99800009 00945 sas 1 1998.12.24
106.580
03303280 1997.11.24 1500 9 9 99800009 01130 sas 1 1998.12.24
11.128
03303280 1997.11.24 1500 9 9 99800009 90095 sas 1 1998.12.24
517.000
03303280 1997.11.24 1500 9 9 99800009 01020 sas 1 1998.12.24
52.000
03303280 1997.11.24 1500 9 9 99800009 01060 sas 1 1998.12.24
4.950
03303280 1997.11.24 1510 R 9 99800018 00095 sas 1 1998.12.24
536.000
03303280 1997.12.22 1300 9 9 99800010 01060 sas 1 1998.12.24
2.380
03303280 1997.12.22 1300 9 9 99800010 01090 sas 1 1998.12.24
25.750
03303280 1997.12.22 1308 Q 2 99800005 99104 sas 6 1998.12.24
03303280 1997.11.24 1500 9 9 99800009 S2001 sas 7 1998.12.24
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: 27 Oct 1998 19:49:59 GMT
From: bhilton@tsg.adc.com (Brand Hilton)
Subject: Re: newbie needs help with file comparison loop 2
Message-Id: <715857$9qv5@mercury.adc.com>
In article <7151k3$2g3$1@nnrp1.dejanews.com>,
<gsechen2@my-dejanews.com> wrote:
>Hello,
>
>I'm very new to perl (and programming) yet I have been assigned to write a
>program.
Jeez, that's kind of a nasty trick to play on somebody.
>I have two data files both datafiles need to be read in. The first datafile
>needs to be compared (line-by-line) to the second datafile. If a line of data
>in the first datafile matches any line in the second data file I need it to
>print the line and the line number.
>
>Below is what I have come up with so far. I seem to have the most trouble
>with the nested while loops that Im trying to use to do the line-by-line
>comparison.
Well, sorry to have to tell you, but there are several things wrong
with this program. But then, I think you probably already know this
since it won't even compile as written. I'll start by correcting the
existing program and then I'll tell you the way I would do it.
First off, always "use strict" and the -w flag. It'll catch a lot of
errors for you.
>##test1.pl###############################
>
>$infile1 = '/adm/gsechen/rick/sta/03303280_rda';
>open (IN1, "$infile1") || die "Cannot create $infile1: $!";
>
>$infile2 = '/adm/gsechen/rick/anomaly.rdb';
>open (IN2, "$infile2") || die "Cannot create $infile2: $!";
First off, congratulations on checking the return code from open. You
have just earned one brownie point. However, you don't need the
double-quotes around $infile1 and $infile2 in the open call. And your
error message is misleading, seeing as how you're not trying to create
files, but open files that already exist.
>$counter = 1;
You probably want to start this at 0, otherwise your line numbers will
all be off by 1.
>while ($line_1 = <IN1>)
You really want "while (defined($line1 = <IN1>))". The -w flag would
have told you this.
>{
> $counter++;
> while ($line_2 = <IN2>)
Ditto here
> {
> if $line_1 = $line_2
You have to put parentheses around the expression here. However, it
still wouldn't do what you want because you're assigning $line_2 into
$line_1. "eq" is what you want. It test strings for equivalence, as
opposed to "==" which tests for numeric equivalence. So, it should
be:
if ($line_1 eq $line2)
> {
> print $line_1;
> print $counter;
This will print out really ugly, but you would have figured that out
for yourself at some point.
> }
> }
>}
How I'd do it, assuming I knew the data files weren't going to be too
terribly big:
#!/usr/local/bin/perl -w
use strict;
my $infile1 = 'doit.in1';
open (IN1, $infile1) || die "Cannot open $infile1: $!";
my $infile2 = 'doit.in2';
open (IN2, $infile2) || die "Cannot open $infile2: $!";
my $counter = 0;
my %lines = map(($_, $counter++), <IN1>);
foreach (<IN2>) {
printf("%5d: %s", $lines{$_}, $_) if exists $lines{$_};
}
Or, if you need the output sorted by line number, replace the foreach
with:
print sort map sprintf("%5d: %s", $lines{$_}, $_),
grep {exists $lines{$_}} <IN2>;
What the heck, I'll even put parentheses in it so you can tell which
things are parameters for which other things:
print(sort(map(sprintf("%5d: %s", $lines{$_}, $_),
grep(exists $lines{$_}, <IN2>))));
Hope that helps somewhat. Of course, now you'll finish the
assignment, and your boss will give you another, more difficult
assignment, and they you'll really be in trouble. I strongly suggest
you get a copy of Learning Perl, and start reading the copious online
documentation that comes with your Perl distribution (man perl, or
perldoc perl for non-Unix systems).
--
_____
|/// | Brand Hilton bhilton@adc.com
| ADC| ADC Telecommunications, ATM Transport Division
|_____| Richardson, Texas
------------------------------
Date: Tue, 27 Oct 1998 17:46:35 GMT
From: gsechen2@my-dejanews.com
Subject: newbie needs help with file comparison loop
Message-Id: <7150tr$14d$1@nnrp1.dejanews.com>
Hello,
I'm very new to perl (and programming) yet I have been assigned to write a
program.
I have two data files both datafiles need to be read in. The first datafile
needs to be compared (line-by-line) to the second datafile. If a line of data
in the first datafile matches any line in the second data file I need it to
print the line and the line number.
Below is what I have come up with so far. I seem to have the most trouble
with the nested while loops that Im trying to use to do the line-by-line
comparison.
Thanks,
-George
gsechen@usgs.gov
##test1.pl###############################
$infile1 = '/adm/gsechen/rick/sta/03303280_rda';
open (IN1, "$infile1") || die "Cannot create $infile1: $!";
$infile2 = '/adm/gsechen/rick/anomaly.rdb';
open (IN2, "$infile2") || die "Cannot create $infile2: $!";
$counter = 1;
while ($line_1 = <IN1>)
{
$counter++;
while ($line_2 = <IN2>)
{
if $line_1 = $line_2
{
print $line_1;
print $counter;
}
}
}
## anomaly.rdb'#################################
#the following data is/should be tab delimited
staid date time medim stype sampl pcode source issue report-
date old-remark old-value
15S 10D 7N 1S 1S 8S 6S 5S 1S 10D 1S 8N<prec>3
03303280 1997.11.24 1500 9 9 99800009 00095 sas 1 1998.12.24
536.000
03303280 1997.11.24 1500 9 9 99800009 00930 sas 1 1998.12.24
35.434
03303280 1997.11.24 1500 9 9 99800009 00945 sas 1 1998.12.24
106.580
03303280 1997.11.24 1500 9 9 99800009 01130 sas 1 1998.12.24
11.128
03303280 1997.11.24 1500 9 9 99800009 90095 sas 1 1998.12.24
517.000
03303280 1997.11.24 1500 9 9 99800009 01020 sas 1 1998.12.24
52.000
03303280 1997.11.24 1500 9 9 99800009 01060 sas 1 1998.12.24
4.950
03303280 1997.11.24 1510 R 9 99800018 00095 sas 1 1998.12.24
536.000
03303280 1997.12.22 1300 9 9 99800010 01060 sas 1 1998.12.24
2.380
03303280 1997.12.22 1300 9 9 99800010 01090 sas 1 1998.12.24
25.750
03303280 1997.12.22 1308 Q 2 99800005 99104 sas 6 1998.12.24
03303280 1997.11.24 1500 9 9 99800009 S2001 sas 7 1998.12.24
03378500 1997.11.04 1130 9 9 99800001 01020 sas 1 1998.12.24
153.890
03378500 1997.11.04 1130 9 9 99800001 01060 sas 1 1998.12.24
7.700
03378500 1997.11.04 1130 9 9 99800001 01106 sas 1 1998.12.24
1.510
03378500 1997.12.16 1150 9 9 99800003 01056 sas 1 1998.12.24
8.540
03378500 1997.12.16 1150 9 9 99800003 01090 sas 1 1998.12.24
8.380
03609750 1997.11.05 1215 9 9 99800002 00608 sas 1 1998.12.24
0.172
03609750 1997.12.17 1230 9 7 99800004 01090 sas 4 1998.12.24
1.140
03609750 1997.12.17 1240 R 7 99800008 01090 sas 4 1998.12.24
12.050
03612500 1997.11.06 1140 9 9 99800005 00608 sas 1 1998.12.24
0.159
03612500 1997.11.06 1140 9 9 99800005 01030 sas 1 1998.12.24
1.460
03612500 1997.11.06 1140 9 9 99800005 01060 sas 1 1998.12.24
2.410
03612500 1997.11.06 1140 9 9 99800005 01090 sas 1 1998.12.24
2.680
03612500 1997.11.06 1140 9 9 99800005 01106 sas 1 1998.12.24
3.220
03612500 1997.12.18 1230 9 9 99800006 01060 sas 1 1998.12.24
2.560
03612500 1997.12.18 1230 9 9 99800006 01090 sas 1 1998.12.24
4.380
03612500 1997.12.18 1230 9 9 99800006 01106 sas 1 1998.12.24
3.240
03612500 1997.12.18 1238 Q 2 99800007 99104 sas 6 1998.12.24
##03303280_rda#############################################
#the following data is/should be tab delimited
staid date time medim stype sampl pcode source issue report-
date old-remark old-value
15S 10D 7N 1S 1S 8S 6S 5S 1S 10D 1S 8N<prec>3
03303280 1997.11.24 1500 9 9 99800009 00095 sas 1 1998.12.24
536.000
03303280 1997.11.24 1500 9 9 99800009 00930 sas 1 1998.12.24
35.434
03303280 1997.11.24 1500 9 9 99800009 00945 sas 1 1998.12.24
106.580
03303280 1997.11.24 1500 9 9 99800009 01130 sas 1 1998.12.24
11.128
03303280 1997.11.24 1500 9 9 99800009 90095 sas 1 1998.12.24
517.000
03303280 1997.11.24 1500 9 9 99800009 01020 sas 1 1998.12.24
52.000
03303280 1997.11.24 1500 9 9 99800009 01060 sas 1 1998.12.24
4.950
03303280 1997.11.24 1510 R 9 99800018 00095 sas 1 1998.12.24
536.000
03303280 1997.12.22 1300 9 9 99800010 01060 sas 1 1998.12.24
2.380
03303280 1997.12.22 1300 9 9 99800010 01090 sas 1 1998.12.24
25.750
03303280 1997.12.22 1308 Q 2 99800005 99104 sas 6 1998.12.24
03303280 1997.11.24 1500 9 9 99800009 S2001 sas 7 1998.12.24
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Tue, 27 Oct 1998 14:00:02 -0500
From: John Porter <jdporter@min.net>
Subject: Re: newbie needs help with file comparison loop
Message-Id: <36361832.955A84DB@min.net>
gsechen2@my-dejanews.com wrote:
>
> I have two data files both datafiles need to be read in. The first datafile
> needs to be compared (line-by-line) to the second datafile. If a line of data
> in the first datafile matches any line in the second data file I need it to
> print the line and the line number.
>
> Below is what I have come up with so far. I seem to have the most trouble
> with the nested while loops that Im trying to use to do the line-by-line
> comparison.
Indeed, that is the essence of your problem: you don't want nested
while loops. You only want one while loop.
In the (one) while loop, read lines, one at a time, from file 1, and
inside that loop, very near the top, read ONE line from file 2.
Not looking at your code, but sorta winging it, something like this:
$counter = 0;
while ( <IN1> ) { # assigns to $_
$line1 = $_;
$counter++;
$line2 = <IN2>;
if ( $line1 eq $line2 ) {
print "$counter: $line1";
}
}
Now, going back and looking at your code, I can see several bugs.
1. You initialize the counter to 1, then increment it at the beginning
of the loop. That means that if the very first line in each file
matches, it will print line = 2. So either initialize to 0, or increment
the counter at the end of the loop.
2. You have syntax errors which make me think you have not tried to run
the program you posted. In particular, the line
if $line_1 = $line_2
needs parentheses. But that's just a syntax error. More grievous is
the use of '=' to perform a comparison. You're actually assigning
$line_2 to $line_1 !!! Not good at all! You would use '==' to compare
them if they were numbers, but they're strings, so compare them with 'eq',
as I have done above.
3. The way you print out on match is pretty weird too. Keep in mind that
the input lines still contain their terminating newlines; so you probably
want to put the counter (the line number) in front. Otherwise, you'd have
to chomp the newline from the input string, then add your own after the
line number, like so:
chomp $line_1;
print "$line_1 $counter\n";
hth,
John Porter
------------------------------
Date: Tue, 27 Oct 1998 11:16:03 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: newbie needs help with file comparison loop
Message-Id: <MPG.109fbbd27a489f4698983f@nntp.hpl.hp.com>
In article <36361832.955A84DB@min.net> on Tue, 27 Oct 1998 14:00:02 -
0500, John Porter <jdporter@min.net> says...
> gsechen2@my-dejanews.com wrote:
> > I have two data files both datafiles need to be read in. The first datafile
> > needs to be compared (line-by-line) to the second datafile. If a line of data
> > in the first datafile matches any line in the second data file I need it to
> > print the line and the line number.
...
> ... You only want one while loop.
> In the (one) while loop, read lines, one at a time, from file 1, and
> inside that loop, very near the top, read ONE line from file 2.
> Not looking at your code, but sorta winging it, something like this:
>
> $counter = 0;
> while ( <IN1> ) { # assigns to $_
> $line1 = $_;
> $counter++;
> $line2 = <IN2>;
>
> if ( $line1 eq $line2 ) {
> print "$counter: $line1";
> }
> }
Your code doesn't meet the problem statement, which calls for a match of
a line in the first data file against *any* line in the second data
file. That can be done best by readng the second data file into a hash,
as you know.
If a line-by-line comparison was required, I think one of the 'diff'
solutions posted recently might be best.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 27 Oct 1998 12:49:59 -0700
From: Arne Jamtgaard <arnej@fc.hp.com>
Subject: Re: newbie needs help with file comparison loop
Message-Id: <363623E7.7AFC@fc.hp.com>
gsechen2@my-dejanews.com wrote:
>
> Hello,
>
> I'm very new to perl (and programming) yet I have been assigned to
> write a program.
> I have two data files both datafiles need to be read in. The first
> datafile needs to be compared (line-by-line) to the second datafile.
> If a line of data in the first datafile matches any line in the
> second data file I need it to print the line and the line number.
> Below is what I have come up with so far. I seem to have the most
> trouble with the nested while loops that Im trying to use to do the
> line-by-line comparison.
> Thanks,
> -George
> gsechen@usgs.gov
> ##test1.pl###############################
> $infile1 = '/adm/gsechen/rick/sta/03303280_rda';
> open (IN1, "$infile1") || die "Cannot create $infile1: $!";
> $infile2 = '/adm/gsechen/rick/anomaly.rdb';
> open (IN2, "$infile2") || die "Cannot create $infile2: $!";
> $counter = 1;
> while ($line_1 = <IN1>)
> {
> $counter++;
> while ($line_2 = <IN2>)
> {
> if $line_1 = $line_2
> {
> print $line_1;
> print $counter;
> }
> }
> }
>
> ## anomaly.rdb'#################################
Okay, I can see a few problems here.
1) Your inner loop (reading from IN2) is going to run out of
things to read after checking the first line of IN1.
2) You're doing an assign (=) instead of a comparison (==)
in your if statement. (You're also missing parens!)
Why not:
Read the second file into a single string, and then step
through the first file, line by line, and check for the
presence of the line in the big string?
To read the whole second file into a single string, check
out the $/ special variable (p. 132 in Blue Camel).
To check for the presence of a string in another string,
check either the index function (p.179 BC) or the match
function (p.145 BC)
HTH!
Arne
------------------------------
Date: 27 Oct 1998 18:11:03 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: Not to start a language war but..
Message-Id: <7152bn$ds$1@mathserv.mps.ohio-state.edu>
[A complimentary Cc of this posting was sent to Dennis Lee Bieber
<wlfraed@ix.netcom.com>],
who wrote in article <3635f671.2262142@nntp.ix.netcom.com>:
> On Tue, 27 Oct 1998 12:39:28 GMT, Brent Michalski
> <perlguy@technologist.com> declaimed the following in comp.lang.python:
> > I have not found a SINGLE real world computing problem that I have not
> > been able to find a way to do it with Perl. I write for the "web world"
>
> By definition, a Turing machine would do...
Please code xfig using your favorite Turing machine implementation.
Ilya
------------------------------
Date: 27 Oct 1998 18:21:18 GMT
From: dformosa@zeta.org.au (David Formosa)
Subject: Re: Not to start a language war but..
Message-Id: <slrn73c3ou.u9i.dformosa@godzilla.zeta.org.au>
In article <3635f671.2262142@nntp.ix.netcom.com>, Dennis Lee Bieber wrote:
> By definition, a Turing machine would do...
Yes but getting thouse infinitly long streams of paper has always been
a difficalt task.
--
Please excuse my spelling as I suffer from agraphia. See the URL in my
header to find out more.
------------------------------
Date: Tue, 27 Oct 1998 14:10:36 -0500
From: John Call <johnc@interactive.ibm.com>
Subject: Re: Not to start a language war but..
Message-Id: <36361AAC.7D66E7B5@interactive.ibm.com>
Abigail wrote:
> Klaus Schilling (Klaus.Schilling@home.ivm.de) wrote on MDCCCLXXXIII
> September MCMXCIII in <URL:news:87vhl671tf.fsf@ivm.de>:
> ++
> ++
> ++ Would python or perl change severely if one replaced ref-counting with a real
> ++ gc like Boehm's?
>
> No.
Speaking of wars, remember the "Burger-wars"? Where's the beef?
Use the language that has the beef. Perl has the beef sometimes and so does Python.
Neither has the beef all the time.
As for me and what I do, Perl has the BEEF!
Thanks,
John
------------------------------
Date: 27 Oct 1998 17:33:04 GMT
From: psl@euclid.jpl.nasa.gov (Peter Scott)
Subject: Re: Perl & Y2K - booby trap code
Message-Id: <71504g$eco@netline.jpl.nasa.gov>
> However, the C documentation for 'struct tm' has *always* stated clearly
> that the year value was 'years since 1900'. So the right answer was
> always just one little indirection away. :-)
Anecdote: years and years ago, I wrote a VMS application which used
localtime(). Recently, that group contacted me to inform me that the
application was not Y2K compliant and could I fix it? I tracked the
problem down to the use of the year field from the struct tm with
century windowing code. I couldn't understand why I would have made
this error, even on what may have been my first use of localtime().
Fortunately, they still had the old VMS C RTL manuals around that I
used at the time. So I looked up localtime. There it was:
"tm_year: last 2 digits of year".
To this day I don't know whether the documentation was wrong or whether
old versions of the VMS C RTL implemented tm_year wrong...
--
This is news. This is your | Peter Scott, NASA/JPL/Caltech
brain on news. Any questions? | (psl@euclid.jpl.nasa.gov)
Disclaimer: These comments are the personal opinions of the author, and
have not been adopted, authorized, ratified, or approved by JPL.
------------------------------
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 4083
**************************************