- The error message you are getting means that you never declared a variable named "@fields" which first appears on line 10. I am guessing you intend for that array to be initialized with the results from your split on line 9.
- If you use warnings; as well, line 9 spits out a warning because you are attempting to store the array result of a split in a scalar.
- Your repeated use of the phrase 'komp_dir' to represent multiple varibles (both scalars and arrays and in multiple scopes) makes reading the code significantly more challenging than it need be.
- Good practice says you should make sure a regex matches before attempting to interact with its contents. There's nothing wrong a priori with what you are doing there, though.
- Your second parenthetical expression in your regex is never used, so by putting those parentheses in you are creating unnecessary work for the regex engine and potentially adding confusion.
- You only need to use next in a loop if you are breaking the natural flow. You are not doing that, so it is unnecessary.
If you are not interested in anything other than the number component of all subdirectories, I would recommend skipping the splitting all together, and perhaps this would be more of what you need:
#!/usr/bin/perl
use strict;
use warnings;
my $path ='/Users/mydirectory/Desktop/BioinfDev/SequenceAssemblyProjec
+t/KOMP/';
my @komp_dir = glob("$path\*");
foreach my $entry (@komp_dir) {
if (-d $entry ){
if ($entry =~ /\/(\d+)_\w*$/) {
print "$1\n";
}
}
}
or, more succinctly
#!/usr/bin/perl -w
use strict;
my $path = '/Users/mydirectory/Desktop/BioinfDev/SequenceAssemblyProje
+ct/KOMP/*';
foreach (glob("$path\*")) {
print "$1\n" if (-d and /\/(\d+)_\w*$/);
}
As a final note, apparently I type vvvveerrryyy ssssllllooowwwlllllyyyyy....
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.