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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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....

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