Repost Search

J

Jimmy

while( <FILEHANDLE> or <FILEHANDLETWO>)
{
$mesAlertes = <FILEHANDLE>;
$mesAlertesDeux = <FILEHANDLETWO>;
$final = ($mesAlertes, $mesAlertesDeux);

print $final;
}

Want to make a search in two files, and print data on screen. But if
the files have the same data, the data will be display once. My code
don't work. why?

BTW, i'm a newbie, so thanks to be patient :)
 
T

Tom Wong

Jimmy said:
while( <FILEHANDLE> or <FILEHANDLETWO>)
{
$mesAlertes = <FILEHANDLE>;
$mesAlertesDeux = <FILEHANDLETWO>;
$final = ($mesAlertes, $mesAlertesDeux);

$file = $mesAlertes;
if( $file ne $mesAlertesDeux) { $file .= $mesAleresDeux }
print $final;
}

Want to make a search in two files, and print data on screen. But if
the files have the same data, the data will be display once. My code
don't work. why?

BTW, i'm a newbie, so thanks to be patient :)

Tom
ztml.com
 
D

David K. Wall

From http://mail.augustmail.com/~tadmc/clpmisc.shtml

Ask perl to help you

You can ask perl itself to help you find common programming
mistakes by doing two things: enable warnings (perldoc warnings)
and enable ``strict''ures (perldoc strict).

That wouldn't help you with this problem, but it will in the future
with other problems.

Picking a better Subject would help, too. "Repost Search" doesn't
actually say much about your question.


It's interesting that 'perldoc -q strict' doesn't find anything. The
use of strictures and warnings is such common advice here in clpm
that you'd think it would be in the FAQ as "Why should I 'use
strict' and 'use warnings'?" Sure, you can look at 'perldoc strict'
and 'perldoc warnings', but those docs are pretty heavy-duty for a
beginner -- and a beginning Perl hacker probably needs them more than
experienced folk.


while( <FILEHANDLE> or <FILEHANDLETWO>)

Reads a line from FILEHANDLE, stores it in $_, then reads a line
from FILEHANDLETWO and stores it in $_, overwriting the line from
FILEHANDLE.

{
$mesAlertes = <FILEHANDLE>;
$mesAlertesDeux = <FILEHANDLETWO>;

If you are at the end of either file, you'll get an error from one
of these lines.
$final = ($mesAlertes, $mesAlertesDeux);

I don't think that does what you seem to think it does. You're using
the comma operator in a scalar context, which will put the value of
$mesAlertesDeux into $final. See 'perldoc perlop' and look for
"Comma Operator". (If it makes you feel better, I made the same
mistake in one of my first posts here and was promptly corrected.)
print $final;
}

Want to make a search in two files, and print data on screen. But
if the files have the same data, the data will be display once. My
code don't work. why?

BTW, i'm a newbie, so thanks to be patient :)

If I understand you correctly, something like this may be what you
want.

[untested]

while(1) {
last if eof(FILEHANDLE) or eof(FILEHANDLETWO);
my $mesAlertes = <FILEHANDLE>;
my $mesAlertesDeux = <FILEHANDLETWO>;
if ($mesAlertes eq $mesAlertesDeux) {
print $mesAlertes;
}
else {
chomp $mesAlertes;
print "*** $mesAlertes *** $mesAlertesDeux";
}
}

This will stop printing when it reaches the end of the file with the
fewest lines.
 
J

John W. Krahn

David K. Wall said:
Reads a line from FILEHANDLE, stores it in $_, then reads a line
from FILEHANDLETWO and stores it in $_, overwriting the line from
FILEHANDLE.

No, nothing gets stored in $_.

perldoc perlop
[snip]
Ordinarily you must assign the returned value to a variable, but
there
is one situation where an automatic assignment happens. If and only
if
the input symbol is the only thing inside the conditional of a
`while'
statement (even if disguised as a `for(;;)' loop), the value is
automatically assigned to the global variable $_, destroying
whatever
was there previously. (This may seem like an odd thing to you, but
you'll use the construct in almost every Perl script you write.) The
$_
variables is not implicitly localized. You'll have to put a `local
$_;'
before the loop if you want that to happen.



John
 
J

Jimmy

Thank you all. It will help alot!

John W. Krahn said:
David K. Wall said:
Reads a line from FILEHANDLE, stores it in $_, then reads a line
from FILEHANDLETWO and stores it in $_, overwriting the line from
FILEHANDLE.

No, nothing gets stored in $_.

perldoc perlop
[snip]
Ordinarily you must assign the returned value to a variable, but
there
is one situation where an automatic assignment happens. If and only
if
the input symbol is the only thing inside the conditional of a
`while'
statement (even if disguised as a `for(;;)' loop), the value is
automatically assigned to the global variable $_, destroying
whatever
was there previously. (This may seem like an odd thing to you, but
you'll use the construct in almost every Perl script you write.) The
$_
variables is not implicitly localized. You'll have to put a `local
$_;'
before the loop if you want that to happen.



John
 
J

John W. Krahn

Jimmy said:
while( <FILEHANDLE> or <FILEHANDLETWO>)
{
$mesAlertes = <FILEHANDLE>;
$mesAlertesDeux = <FILEHANDLETWO>;
$final = ($mesAlertes, $mesAlertesDeux);

print $final;
}

Want to make a search in two files, and print data on screen. But if
the files have the same data, the data will be display once. My code
don't work. why?

BTW, i'm a newbie, so thanks to be patient :)

Well, the simple answer is:

@ARGV = ( 'file1', 'file2' );
my %seen;
while ( <> ) {
print unless $seen{ $_ }++;
}


John
 
J

Jimmy

thank you john. This is just what i needed!

John W. Krahn said:
Well, the simple answer is:

@ARGV = ( 'file1', 'file2' );
my %seen;
while ( <> ) {
print unless $seen{ $_ }++;
}


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,137
Messages
2,570,795
Members
47,342
Latest member
eixataze

Latest Threads

Top