in reply to noob regexpression question

I keep getting compile errors.
Try to put the following at the beginning of your script:
use strict; use warnings; use diagnostics;
and you should get better info about what's going wrong. At a first glance, you're missing the foreach closing brace. Please note that using strict obliges you to declare all your variables before you use them, which you should appreciate as a C++ programmer :)

If each $line contains the whole stuff, you'll have hard times. You should probably split using "," as a separator, then iterate thru the elements:

foreach my $line (@lines) { chomp $line; foreach my $element (split /,/ , $line) { my $extension = substr($element, -3) || ''; if ( $extension eq 'exe' ) { push @dllarray, "[$element]"; } elsif ( $extension eq 'dll' ) { push @dllarray, $element; } } }
Note: CODE UNTESTED!!!

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

Replies are listed 'Best First'.
Re^2: noob regexpression question
by ikegami (Patriarch) on Nov 07, 2005 at 17:10 UTC
    The || '' doesn't help, and you'll fail for "foodll".
    ... my $extension = substr($element, -3) || ''; if ( $extension eq 'exe' ) { ... elsif ( $extension eq 'dll' ) ...
    should be
    ... my $extension = substr($element, -4); if ( $extension eq '.exe' ) { ... elsif ( $extension eq '.dll' ) ...
      D'ho, lucky me that I put the big "untested code" :) I was convinced that the substr function would return undef if the string was empty (like the middle element in "ciccio.exe,,pallo.dll"), but it is simply the empty string.

      I could agree on the second correction, but only on a robustness ground that seems not necessary in this case. Anyway, better being overprotective, especially when it's cheap and helps building the right attitude.

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.