in reply to warnings and grep problem

Something doesn't quite . . . match up. Calling grep on %$packlist rather than (say) keys %$packlist seems odd. Not to mention you seem to be trying to use grep in one step where you probably want a map of grep'd results. Modifying things inside of a grep is . . . unusual; grep is for selecting items from a list which satisfy some predicate, map is for constructing a new list by applying some transformation to the elements of an existing list.

Those things aside, I'd use FIle::Basename to split the paths apart instead of a regex.

Replies are listed 'Best First'.
Re^2: warnings and grep problem
by jeanluca (Deacon) on Jun 20, 2006 at 15:49 UTC
    You're probably right (I'm still not very good with grep and map)
    Anyway I tried the following
    my $hash = map { /\Q$dir\E(\w+?)\.pm$/ and defined $1, $1 } keys %$pac +klist ;
    Looks a lot better,but printing it like
    foreach ( keys %hash) print $_ ; }
    gives a new warning
    Odd number of elements in hash assignment at /usr/lib/p....
    Any suggestions ?

    LuCa

      You're attempting to use the number of keys which matched to initialize a hash. Trying to use a single scalar to initialize a hash isn't going to produce anything useful.

      my @desired_keys = grep { /\Q$dir\E.*?\.pm$/ } keys %$packlist; my %hash; @hash{ map { /\Q$dir\E(\w+?)\.pm$/; $1 } @desired_keys } = @desired_ke +ys;

      Update: Gah. Me no write well. Reworded to (hopefully) clarify.

        Ok, but what about this
        my @dirs = map { s/\Q$dir\E(\w+?)\.pm$/$1/, $1 } keys %$packlist; for (@dirs) { print "|$_|\n" if ( $_ and length($_) > 2 ); }
        This seems to work, but without the 'length($_) > 2' I get
        |1| |A_Module| |1| |An_Other_Module|
        Is there a better way to get rid of the 1s!

      You assigned the output of the map to a scalar $has so you'll just get the number of elements map returned, not the actual elements. Then you did keys %hash which is a whole new variable. The following might do what you want, but if you are messing with path/file names then i would highly recommend use a CPAN module.

      my @dirs = map { s/\Q$dir\E(\w+?)\.pm$/$1/ } keys %$packlist; for (@dirs) { print $_, "\n"; }

      ___________
      Eric Hodges