in reply to Complex string parsing

What tells you that a line corresponds to a directory? The fact that it has a slash on the end of it? Here's a regular expression that will match such entries:

$line =~m#/$#;

But if you have such a marker of directories already, I'm not sure why you'd need to do what you're doing. If the existence and absence of a trailing slash isn't a reliable indicator of what's a directory (or even if you can't *assume* they are), then you'll have to go to the filesystem and use the -d filetest (see this node for more info on filetest operators).

HTH

Replies are listed 'Best First'.
Re: Re: Complex string parsing
by Anonymous Monk on Apr 25, 2001 at 05:34 UTC
    Hi all, thanks for your replies. I was getting stuck on the the regx for the directory match. I also put in a check to see if the path is in fact a directory. The code below does what I was after.
    open(TMP,'file.txt'); while($line = <TMP>) { chomp($line); next if $line =~ /^Total/; next if $line =~ /^\d+/; ($path) = split(/\s+/,$line); if (-d "$path") { if ($path =~ m!/$!) { $dir = (split /\//, $path)[-1]; print "/$dir/=/$dir\n"; } } } close(TMP);
    Again, many thanks! Regards, Stacy.
      Maybe you like this concise style:
      open TMP, 'file.txt'; while( <TMP> ){ chomp; next if /^Total/ or /^\d/; s/\s+//; if (-d and m!/$!){ my $dir = (split !/!)[-1]; print "/$dir/=/$dir\n"; } } close TMP;
      But of course, this is subject to taste.
      "We are not alone"(FZ) Update: greenfox pointed out that I forgot the TMP in the while condition...