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.


In reply to Re: Issue with capturing a string after splitting on "/" by Lawliet
in thread Issue with capturing a string after splitting on "/" by lomSpace

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.