What am I doing wrong?!

T

Tony Walker

Hi Guys,

OK, what I'm doing is a basic search of a logfile for specific
strings. At present I have this working fine by manually creating an
array and foreaching it..
However, I now want to read these strings from a file and perform the
same actions as before but I'm hitting a problem. I slurp the file
into and array and it only ever finds ONE of the strings in the file,
not the other!!

Any help much appreciated..

Tony

p.s this is my first attempt at perl so it may not look great...
##
#!/opt/perl/bin/perl
open (ER,"ALL_errors") or die "$!";
my @entry = <ER>;

open (LOG,"all.log") or die "$!";
while (<LOG>){
my $CUR=$_;
for (my $i=0;$i<=$#entry;$i++) {
my $LINE = @entry[$i];
if ($CUR =~/$LINE/){
print $CUR;
}
}
}
####
 
J

Jürgen Exner

Tony said:
OK, what I'm doing is a basic search of a logfile for specific
strings. At present I have this working fine by manually creating an
array and foreaching it..
However, I now want to read these strings from a file and perform the
same actions as before but I'm hitting a problem. I slurp the file
into and array and it only ever finds ONE of the strings in the file,
not the other!!

Any help much appreciated..

Tony

p.s this is my first attempt at perl so it may not look great...
##
#!/opt/perl/bin/perl
open (ER,"ALL_errors") or die "$!";
my @entry = <ER>;

I wager you forgot to chomp() here.
open (LOG,"all.log") or die "$!";
while (<LOG>){
my $CUR=$_;
for (my $i=0;$i<=$#entry;$i++) {

Och, that's C, not Perl:
for (my $i=0..@entry)

Or use just
for (@entry)
my $LINE = @entry[$i];
if ($CUR =~/$LINE/){

and then here
if ($CUR =~/$_/){
instead fo the two lines above
 
T

Tore Aursand

#!/opt/perl/bin/perl

use strict;
use warnings;
use diagnostics;
open (ER,"ALL_errors") or die "$!";
my @entry = <ER>;

open (LOG,"all.log") or die "$!";
while (<LOG>){
my $CUR=$_;
for (my $i=0;$i<=$#entry;$i++) {
my $LINE = @entry[$i];
if ($CUR =~/$LINE/){
print $CUR;
}
}
}

Very C'ish, indeed. Let's do the same thing Perl'ish;

open( ER, 'ALL_errors' ) or die "Couldn't open file; $!\n";
my @entry = <ER>;
close( ER );
chomp( @entry );

open( LOG, 'all.log' ) or die "Couldn't open file; $!\n";
while my $cur ( <LOG> ) {
chomp( $cur );
foreach my $line ( @entry ) {
print $cur if ( $cur =~ /$line/ );
}
}
close( LOG );
 
M

Michael Budash

Hi Guys,

OK, what I'm doing is a basic search of a logfile for specific
strings. At present I have this working fine by manually creating an
array and foreaching it..
However, I now want to read these strings from a file and perform the
same actions as before but I'm hitting a problem. I slurp the file
into and array and it only ever finds ONE of the strings in the file,
not the other!!

Any help much appreciated..

Tony

p.s this is my first attempt at perl so it may not look great...
##
#!/opt/perl/bin/perl
open (ER,"ALL_errors") or die "$!";
my @entry = <ER>;

open (LOG,"all.log") or die "$!";
while (<LOG>){
my $CUR=$_;
for (my $i=0;$i<=$#entry;$i++) {
my $LINE = @entry[$i];
if ($CUR =~/$LINE/){
print $CUR;
}
}
}
####

####
open (ER,"ALL_errors") or die "$!";
my @entry = <ER>;
close (ER);

chomp @entry;

open (LOG,"all.log") or die "$!";
while (<LOG>){
foreach my $entry (@entry) {
print if /$entry/;
}
}
close (LOG);
####

hope this helps
 
D

David K. Wall

Tore Aursand said:
Very C'ish, indeed. Let's do the same thing Perl'ish;

open( ER, 'ALL_errors' ) or die "Couldn't open file; $!\n";
my @entry = <ER>;
close( ER );
chomp( @entry );

open( LOG, 'all.log' ) or die "Couldn't open file; $!\n";
while my $cur ( <LOG> ) {
chomp( $cur );
foreach my $line ( @entry ) {
print $cur if ( $cur =~ /$line/ );
}
}
close( LOG );

That has far more parentheses than I would normally use. Are you sure
it isn't Lisp-ish? :)
 
T

Tony Walker

Guys,

Thanks for all the help! Can't believe it was a chomp() that was my problem.

A question related to one of the responses:

I'm now trying

for (my $i=0..@entry)
or
for (@entry)


And neither seem to be working??

Thanks again,

Tony
 
R

Roy Johnson

I'm now trying

for (my $i=0..@entry)
or
for (@entry)

And neither seem to be working??

You may have two loops that use $_ as their loop variables. You need
to post the whole section of code and say *how* it's not working. The
code in the example to which you replied looked good to me.
 
J

Jürgen Exner

Tony said:
A question related to one of the responses:

I'm now trying

for (my $i=0..@entry)

Sorry, that was from me and it was rather inconsidered of me to post this
piece code without testing.
What I meant was

for my $i (0..$#entry)
or
for (@entry)


And neither seem to be working??

This is a useless error description.
Both are working fine as standalone lines of code, but they may not be doing
what you think they are doing or they may not be doing in context what you
think they are doing.

Without you telling us what you except them to do and without showing us
your new program code there is no way to tell why you they behave different
then you expect (except that I still remembered that my piece of code had
the right idea, but was coded wrong).

jue
 
T

Tore Aursand

I'm now trying

for (my $i=0..@entry)
or
for (@entry)

And neither seem to be working??

Use 'foreach';

foreach ( @array ) {
}

If you need to know - or count - the element index, just use a counter;

my $cnt = 0;
foreach ( @array ) {
$cnt++;
print $_ . ' is element #' . $cnt . "\n";
}
 
D

David K. Wall

Jürgen Exner said:
What exactly would be the difference between 'for' and 'foreach'
(besides the additional 4 letters)?

It's a bit more explicit about what it's doing? perlsyn gives
'readability' as the justification for the existence of 'foreach', but
that's arguable, and I don't really see the need for it. 'foreach' is
a bit of dead yeast left in the Perl beer, IMHO; forget the yeast and
enjoy the flavor. :)
 
T

Tore Aursand

What exactly would be the difference between 'for' and 'foreach'
(besides the additional 4 letters)?

I would say readability, plus that it simply works better than writing
'for (my $i = 0..@array)' (if you had bothered quoting the relevant text
from my post.
 
B

Ben Morrow

Tore Aursand said:
I would say readability,

Debatable, but yes.
plus that it simply works better than writing
'for (my $i = 0..@array)'

No. The problem there is nothing to do with for/foreach. foreach can
in every single case be replaced with for.

Ben
 
T

Tore Aursand

No. The problem there is nothing to do with for/foreach. foreach can
in every single case be replaced with for.

I know that, but look at the actual code. Maybe I'm misunderstanding
something here, but have tried this?

my @array = qw( a b c d e f );
for ( my $i = 0 .. @array ) {
print $i . ' = ' . $array[ $i ] . "\n";
}

Doesn't look nice to me.
 
J

Jeff Boes

Tony said:
my $LINE = @entry[$i];
if ($CUR =~/$LINE/){
print $CUR;
}

Consider "$CUR =~ /\Q$LINE\E/" here; if the text in $LINE contains any
regular expression meta-characters (like "."), your match may not work
the way you want it to.
 
T

Tony Walker

Jeff Boes said:
Tony said:
my $LINE = @entry[$i];
if ($CUR =~/$LINE/){
print $CUR;
}

Consider "$CUR =~ /\Q$LINE\E/" here; if the text in $LINE contains any
regular expression meta-characters (like "."), your match may not work
the way you want it to.

Thanks for all the input Gentlemen. Since posting my rather 'useless'
error description I have cracked the problem and the script I
originally posted this message about is running nicely without
problems. Nice to have found such a rich source of information...

Tony
 
A

Anno Siegel

Tore Aursand said:
No. The problem there is nothing to do with for/foreach. foreach can
in every single case be replaced with for.

I know that, but look at the actual code. Maybe I'm misunderstanding
something here, but have tried this?

my @array = qw( a b c d e f );
for ( my $i = 0 .. @array ) {
print $i . ' = ' . $array[ $i ] . "\n";
}

Doesn't look nice to me.

No, but it doesn't work with foreach either.

While "for" and "foreach" are really synonyms, there are still two
entirely different loop constructs they can *both* introduce. The
difference is in the syntax of what follows for/foreach.

Anno
 
T

Tore Aursand

my @array = qw( a b c d e f );
for ( my $i = 0 .. @array ) {
print $i . ' = ' . $array[ $i ] . "\n";
}

Doesn't look nice to me.
No, but it doesn't work with foreach either.

I never said that. I just said that one should use...

foreach ( @array ) {
...
}

....instead of the code above. I know that 'for' and 'foreach' do excactly
the same.
 
A

Anno Siegel

Tore Aursand said:
my @array = qw( a b c d e f );
for ( my $i = 0 .. @array ) {
print $i . ' = ' . $array[ $i ] . "\n";
}

Doesn't look nice to me.
No, but it doesn't work with foreach either.

I never said that. I just said that one should use...

foreach ( @array ) {
...
}

But the code above isn't just bad, or hard to read, it's *wrong*. It
doesn't loop over the array. Instead it assigns the result of "scalar
0 .. @array" to $i (once), assigns the result to $_, and executes the
loop body (once).

(Apart from that, you'd want $#array instead of @array, but that doesn't
matter anymore.)

Anno
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top