ckaspar has asked for the wisdom of the Perl Monks concerning the following question:

How would I write the regular expression - if (grep(/^$check!/,$_)) $check = file.zip. I need strip the .zip off within the grep. So it would look like if (grep(/^file!/,$_)) with no .zip in the grep.
#!/usr/bin/perl my $scr = "/home/scr/Linux"; my $dest = 1; my $genlist = "$scr/genlist"; my $newlist = "/$scr/Packages/Disk$dest/newlist"; opendir(DIR,"$scr"); @dir_list = grep(/^DVD\d+$/,readdir(DIR)); closedir(DIR); foreach $dir (@dir_list) { opendir(DIR,"$scr/$dir/Packages"); @file_list = grep(!/^\.\.?$/,readdir(DIR)); system("mkdir -p $scr/DVD1/Packages/Disk$dest"); open (LOG, ">$newlist"); foreach $check (@file_list) { open (LIST, "$genlist"); while (<LIST>){ chomp; if (grep(/^$check/,$_)) { print LOG "$_\n"; } } } $dest++ } close (LIST); closedir(DIR);

Replies are listed 'Best First'.
Re: Question about grep
by GrandFather (Saint) on Mar 20, 2006 at 04:18 UTC

    if (grep(/^$check/,$_)) { is a rather interesting line. In essence it is the equivelent of if (/^$check/) { because there is only one element in the list for grep to work on ($_).

    That aside, you need to strip the .zip off before the test, not during the test. One way (there are many) would be:

    my $check = 'test.zip'; (my $test = $check) =~ s/\.zip$//; # strip the zip $_ = 'test'; print "match" if /^$test/; # Note no grep - no need

    DWIM is Perl's answer to Gödel
Re: Question about grep
by helphand (Pilgrim) on Mar 20, 2006 at 04:10 UTC
Re: Question about grep
by ahmad (Hermit) on Mar 20, 2006 at 16:56 UTC

    Make it this way

    foreach $check (@file_list) { $check=~s/\.zip//;