in reply to Issue with capturing a string after splitting on "/"

Addressing the immediate problem, I would say that you have no array called @fields and therefore cannot access the seventh element of it.

Delving further, I am confused as to what you are doing. For one, you assign $komp_dir the directory and then never use it. (However, I assume there is more to the program.) Continuing on, let me translate your code to English to see if that is what you want.

foreach $file in @directory { if the $file is a directory { assign an implicit split to @_ (deprecated) to $file; # hmmm assign the seventh element of @fields to $file and match a reg +ex; # hmmm print the capture from the previous regex; explicitly go to the next element; } }

I think you want my @path = split(/\//, $komp_dir) instead of your split. I do not really know what you want for the other hmmm'd line. Assuming you want what shmen had written, a simple rindex will suffice. (And you can condense the if statement to one line!) you can use File::Basename:

use File::Basename; my $fname = basename($path);

Update: added three final sentences after reading shemn's post.

Update: rindex method is not so simple, added easier method.

And you didn't even know bears could type.

Replies are listed 'Best First'.
Re^2: Issue with capturing a string after splitting on "/"
by lomSpace (Scribe) on Feb 06, 2009 at 15:02 UTC

    Hi Lawliet!
    Yes, I want what shmen as written. This will likely become
    a subroutine in a program that I am working on. I have to cp files
    to a dir. The beginning of the files and dir share
    the same title, ie 13323. So I have to capture digits at the beginning of the dir name.