in reply to unitialized value error in pattern matching script

I'm shocked that no-one mentioned the really obvious solution - don't use a regex!
my $i = 0; while (my ($args1, $args2) = split('.', $_myfile[$i++])) { print "$args2\n"; }
If you're parsing, a regex should be your last resort. You should have at least considered the following options first:
  1. Judicious use of split. (In the case above, we would've needed another split.) Use this if you have delimiters (like a period?)
  2. Judicious use of unpack. Use this if your data is column-formatted. (Most work with mainframes or network protocols will use unpack, for example.)
  3. Now, and only now, should regexes be used. Regexes should be used if the above do not apply. They are harder to maintain and, if you don't know what you're doing, can be the cause of subtle bugs.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re: pattern matching
by gellyfish (Monsignor) on Mar 29, 2002 at 15:02 UTC

    Er, hang on, the first argument to split is a regular expression - however it is quoted. Your snippet will split on any any character - and thus nothing will be assigned to $args1 and $args2. This can be demonstrated by the following :

    #!/usr/bin/perl -w use strict; my $string = 'this is a test'; my @foo = split('(.)', $string); print join('*',@foo);
    If you read the perlfunc entry for split you will see that using the '()' in the regular expression includes the delimiting character in the output substrings.

    /J\