What am I doing wrong?!

Discussion in 'Perl Misc' started by Tony Walker, Oct 29, 2003.

  1. Tony Walker

    Tony Walker Guest

    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;
    }
    }
    }
    ####
    Tony Walker, Oct 29, 2003
    #1
    1. Advertising

  2. Tony Walker wrote:
    > 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

    > print $CUR;
    > }
    > }
    > }
    > ####
    Jürgen Exner, Oct 29, 2003
    #2
    1. Advertising

  3. Tony Walker

    Tore Aursand Guest

    On Wed, 29 Oct 2003 08:36:05 -0800, Tony Walker wrote:
    > #!/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 );


    --
    Tore Aursand <>
    Tore Aursand, Oct 29, 2003
    #3
  4. In article <>,
    (Tony Walker) wrote:

    > 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
    --
    Michael Budash
    Michael Budash, Oct 29, 2003
    #4
  5. Tore Aursand <> wrote:

    > 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? :)

    --
    David Wall
    David K. Wall, Oct 29, 2003
    #5
  6. Tony Walker

    Tony Walker Guest

    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
    Tony Walker, Oct 30, 2003
    #6
  7. Tony Walker

    Roy Johnson Guest

    (Tony Walker) wrote in message news:<>...
    > 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.
    Roy Johnson, Oct 30, 2003
    #7
  8. Tony Walker wrote:
    > 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
    Jürgen Exner, Oct 30, 2003
    #8
  9. Tony Walker

    Tore Aursand Guest

    On Thu, 30 Oct 2003 02:12:54 -0800, Tony Walker wrote:
    > 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";
    }


    --
    Tore Aursand <>
    Tore Aursand, Oct 30, 2003
    #9
  10. Tore Aursand wrote:
    > On Thu, 30 Oct 2003 02:12:54 -0800, Tony Walker wrote:
    >> I'm now trying
    >> for (@entry)
    >>

    > Use 'foreach';
    > foreach ( @array )


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

    jue
    Jürgen Exner, Oct 30, 2003
    #10
  11. Jürgen Exner <> wrote:

    > Tore Aursand wrote:
    >> On Thu, 30 Oct 2003 02:12:54 -0800, Tony Walker wrote:
    >>> I'm now trying
    >>> for (@entry)
    >>>

    >> Use 'foreach';
    >> foreach ( @array )

    >
    > 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. :)

    --
    David Wall
    David K. Wall, Oct 30, 2003
    #11
  12. Tony Walker

    Tore Aursand Guest

    On Thu, 30 Oct 2003 18:10:17 +0000, Jürgen Exner wrote:
    >>> I'm now trying
    >>> for (@entry)


    >> Use 'foreach';
    >> foreach ( @array )


    > 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.


    --
    Tore Aursand <>
    Tore Aursand, Oct 31, 2003
    #12
  13. Tony Walker

    Ben Morrow Guest

    Tore Aursand <> wrote:
    > On Thu, 30 Oct 2003 18:10:17 +0000, Jürgen Exner wrote:
    > >>> I'm now trying
    > >>> for (@entry)

    >
    > >> Use 'foreach';
    > >> foreach ( @array )

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

    >
    > 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

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
    Ben Morrow, Oct 31, 2003
    #13
  14. Tony Walker

    Tore Aursand Guest

    On Fri, 31 Oct 2003 07:23:24 +0000, Ben Morrow wrote:
    >> 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.


    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.


    --
    Tore Aursand <>
    Tore Aursand, Oct 31, 2003
    #14
  15. Tony Walker

    Jeff Boes Guest

    Tony Walker wrote:
    > 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.

    --
    Jeff Boes vox 269.226.9550 ext 24
    Database Engineer fax 269.349.9076
    Nexcerpt, Inc. http://www.nexcerpt.com
    ...Nexcerpt... Extend your Expertise
    Jeff Boes, Oct 31, 2003
    #15
  16. Tony Walker

    Tony Walker Guest

    Jeff Boes <> wrote in message news:<>...
    > Tony Walker wrote:
    > > 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
    Tony Walker, Nov 3, 2003
    #16
  17. Tony Walker

    Anno Siegel Guest

    Tore Aursand <> wrote in comp.lang.perl.misc:
    > On Fri, 31 Oct 2003 07:23:24 +0000, Ben Morrow wrote:
    > >> 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.

    >
    > 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
    Anno Siegel, Nov 3, 2003
    #17
  18. Tony Walker

    Tore Aursand Guest

    On Mon, 03 Nov 2003 10:51:27 +0000, Anno Siegel wrote:
    >> 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.


    --
    Tore Aursand <>
    Tore Aursand, Nov 3, 2003
    #18
  19. Tony Walker

    Anno Siegel Guest

    Tore Aursand <> wrote in comp.lang.perl.misc:
    > On Mon, 03 Nov 2003 10:51:27 +0000, Anno Siegel wrote:
    > >> 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
    Anno Siegel, Nov 3, 2003
    #19
  20. Tony Walker

    Tore Aursand Guest

    On Mon, 03 Nov 2003 12:37:27 +0000, Anno Siegel wrote:
    > [...]
    > But the code above isn't just bad, or hard to read, it's *wrong*.


    Excactly my point.


    --
    Tore Aursand <>
    Tore Aursand, Nov 3, 2003
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Salisha Khan
    Replies:
    1
    Views:
    11,644
    JohnFol
    Aug 1, 2003
  2. Keith R. Williams

    What am I doing wrong?

    Keith R. Williams, Jul 14, 2003, in forum: VHDL
    Replies:
    4
    Views:
    1,038
    Ed Diego
    Jul 15, 2003
  3. ed
    Replies:
    1
    Views:
    655
  4. Aaron Ackerman
    Replies:
    2
    Views:
    369
    Lewis Wang [MSFT]
    Jul 29, 2003
  5. J

    What am I doing wrong

    J, Aug 14, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    381
    John Saunders
    Aug 14, 2003
Loading...

Share This Page