in reply to RE: Re: Help for awk/regex/newbie
in thread Help for awk/regex/newbie

#!/usr/bin/perl -w use strict; my @cleanarray = <>; foreach (@cleanarray) { print "$1\n" if m/(\S+)/; }
Update: Fixed the loop to only print if the match succeeds, per merlyn

Now that I think about it though, a mock awk really ought to look like this:

#!/usr/bin/perl -w use strict; my @cleanarray = <>; foreach (@cleanarray) { # The regex always matches so there's no need # for a conditional. m/(\S*)/; print "$1\n"; }
That matches awk's behavior on blank lines better than my first stab.

-Matt

Replies are listed 'Best First'.
RE: RE: RE: Re: Help for awk/regex/newbie
by merlyn (Sage) on Aug 14, 2000 at 17:38 UTC
    Awk! (pun intended).

    Never use $1 unless it's within the context of a conditional based on the success of the match.

    Otherwise, you get the previous match. In this case, an empty line would give you a duplicate "first" word from the previous entry!

    -- Randal L. Schwartz, Perl hacker