Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Read the directories from file

by finddata (Sexton)
on Mar 27, 2017 at 07:10 UTC ( #1186042=perlquestion: print w/replies, xml ) Need Help??

finddata has asked for the wisdom of the Perl Monks concerning the following question:

inputfile: base { LOCATION:../set/projects/all/files }
My code: my $fn="inputfile"; open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; while ( <$fh> ) { chomp; if (/^LOCATION:\s*(\S+)/) { $input_dir = $1; print $input_dir; } My output is as follows: ../set/projects/all/files expected one: /set/projects/all/files
In the above code the location line has directory location.These directory location starts with .. How can i remove the .. from the directory line and read only the location of the directory

Replies are listed 'Best First'.
Re: Read the directories from file (updated)
by haukex (Chancellor) on Mar 27, 2017 at 07:49 UTC
    $input_dir =~ s{^\.\./}{/};

    See perlretut.

    However, it seems a bit strange to want to do it this way. If you want to do some advanced filename manipulation, I suggest Path::Class or Path::Tiny, or at the very least the core module File::Spec. All three of these also have the added advantage of being portable across OSes (Path::Tiny only does *NIX and Win32, though).

    use File::Spec::Functions qw/splitdir updir catdir/; my @input_dir = splitdir $input_dir; shift @input_dir if $input_dir[0] eq updir; $input_dir = catdir(@input_dir); # $input_dir is now "set/projects/all/files"

    Update: Corrected {^../} to {^\.\./}.

Re: Read the directories from file
by Discipulus (Abbot) on Mar 27, 2017 at 07:45 UTC
    Hello, you have stil not understood how to compose effictively a node?  <c>..</c> tags are intended to hold code and input/output data: not the whole question.

    You know monks can be very susceptibles after many years passed in their cells sudiyng Scriptures.

    That said your output come from your regular expression: /^LOCATION:\s*(\S+)/ captures ../set/projects/all/files if given LOCATION:../set/projects/all/files as input.

    If you do not want the two dots to be captured you can put it outside capturing parens. Dots must be escaped in regexes because they are special chars. So I guess that /^LOCATION:\s*\.\.(\S+)/ shold not capture the two unwanted dots.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Read the directories from file
by Anonymous Monk on Mar 27, 2017 at 07:34 UTC

    How can i remove the .. from the directory line and read only the location of the directory

    Do it after?

    A more generic parser

    my %action = ( LOCATION => sub { my( $dir ) = @_; $dir =~ s{^\.\.}{}; print $dir; }, ); ... while ... /^ # start of line \s* # optional padding ( [^:]+ ): # $1 key, not ':' \s* ([^\r\n]*) # $2 value, not line end [\r\n]* # end of line /xm and do { my( $key, $value ) = @_; my $action = $parser{ $key } || $parser{default}; $action->($value); };
Re: Read the directories from file
by madtoperl (Friar) on Mar 27, 2017 at 09:48 UTC
    Hi finddata
    Like haukex suggested, there are plenty of modules available which can be used for handling this requirement instead of doing it in manual way.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1186042]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2019-12-14 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?