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

I am trying to build a path out of variables. I am reading lines from a file with a while loop. I then parse the information into variables. Here is my code

my ($var1) = 'stuff_to_cut_out_with_substr_FOO'; my ($var2) = 'stuff_to_cut_out_with_substr_BAR'; my ($var3) = 'stuff_to_cut_out_with_substr_LOST'; if (/foo/) { $var1 = substr($_, 8); } elsif (/bar/) { $var2 = substr($_, 7); } elsif (/lost/) { $var3 = substr($_, 5); } my ($path) = $var1 . '/' . $var2 . '/' / $var3 say "$path";

The problem is var1 is being replaced in the $path variable. The output looks like "obar/lost" instead of "/foo/bar/lost". When I print just the variables $var1, $var2, and $var3 they have the correct data in them. The problem happens when I try to build them into a string. I've also tried the following code

use File::Spec::Functions; my ($path) = catfile($var1, $var2, $var3);

This second code results in the same output. It seems like some sort of pattern matching is going on but I can't figure this out to save my life.

Replies are listed 'Best First'.
Re: Building Paths without Regex
by johngg (Canon) on Sep 01, 2011 at 18:09 UTC

    Be aware that regular expressions are case sensitive; you are looking for "foo" but the string contains "FOO". Also, you seem to be matching against $_ and applying substr to it but your code doesn't show where you initialise it. I'm finding your question a little difficult to understand but perhaps captures and lc would be applicable.

    knoppix@Microknoppix:~$ perl -Mstrict -wE ' > my $var1 = q{some_stuff_FOO}; > my $var2 = q{more_gubbins_BAR}; > my $var3 = q{lose_this_LOST}; > > my $path = join q{/}, > q{}, > map { my( $ext ) = m{([A-Z]+)$}; lc $ext } $var1, $var2, $var3; > say $path;' /foo/bar/lost knoppix@Microknoppix:~$

    I hope I've guessed correctly and this is helpful.

    Cheers,

    JohnGG

      Thank you for the quick answer! I found a typo in the first if statment while I was ensuring that I was doing everything case sensitive. Here is an example of what I was doin wrong:

      if (/bar/){ $foo = substr($_, 5) } elsif (/bar/) { $bar = substr($_, 8) }

      I was testing for /bar/ but adding the variable to $foo, which was messing up the first variable $var1. To answer your question, the $_ is the default value from the while loop used to read the source file. I want to apologize for not making my post clearer. I am reading data from a file using a while loop. I've been at this for 12 hours straight :) and am a little fried

Re: Building Paths without Regex
by Marshall (Canon) on Sep 03, 2011 at 03:10 UTC
    This problem just cries out for regex in Perl.
    This is not a sub-string problem.
    sub string is the wrong tool for the job.
    #!/usr/bin/perl -w use strict; my ($var1) = 'stuff_to_cut_out_with_substr_FOO'; my ($var2) = 'stuff_to_cut_out_with_substr_BAR'; my ($var3) = 'stuff_to_cut_out_with_substr_LOST'; my @paths; foreach my $line ($var1, $var2, $var3) { push @paths, $line =~ /_([A-Za-z]+)$/; #stuff after last _ } print join("/",@paths),"\n"; __END__ prints "FOO/BAR/LOST"