Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Unable to use if condition inside foreach loop using perl?

by finddata (Sexton)
on Mar 23, 2017 at 09:38 UTC ( #1185583=perlquestion: print w/replies, xml ) Need Help??

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

open my $project_file, '<', $input_dir; chomp(my @store_files = <$project_file>); #print @store_files; close $project_file; foreach my $temp_line(@store_files) { if(my $pattern =~ /^REVISION_LOCATION:/){ my @fetch_path=split (':',$temp_line); my $new_path=$fetch_path[1]; print $new_path; last; } } <c> Error: Use of uninitialized value in pattern match (m//)

Replies are listed 'Best First'.
Re: Unable to use if condition inside foreach loop using perl?
by Corion (Pope) on Mar 23, 2017 at 09:41 UTC
    if(my $pattern =~ /^REVISION_LOCATION:/){

    Please explain in plain English what this line is supposed to do.

    Please explain every character or word you used there and describe its meaning and effect.

      There am checking for if the line starts with the word REVISION_LOCATION then the loop as follows.

        Honestly, I think you should review your previous questions, the responses you have had and take the advice given many times about learning the basics. Repeating the same mistakes means you're not learning from them.

        Please explain every character or word you used there and describe its meaning and effect.

        In your reply, I cannot see how you explained for example the word my, the word $pattern_line and some others. Please try again.

          A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Unable to use if condition inside foreach loop using perl?
by haukex (Chancellor) on Mar 23, 2017 at 09:45 UTC

    The =~ operator applies the pattern on the right-hand side to the value on the left-hand side. This means the value on the left-hand side should be the line you are matching against. Try if($temp_line =~ /^REVISION_LOCATION:/)

    Update: Added link

Re: Unable to use if condition inside foreach loop using perl?
by thanos1983 (Parson) on Mar 23, 2017 at 12:18 UTC

    Hello finddata

    I think everyone is trying to explain that the way that you have defined your regular expression will not work. The reason:

    foreach my $temp_line(@store_files) { if(my $pattern =~ /^REVISION_LOCATION:/){

    Simply you are comparing against an empty string. It is like you are doing if('' =~ /^REVISION_LOCATION:/){, when you should be doing something like: if($temp_line =~ /^REVISION_LOCATION:/){.

    If still is not clear enough read the perlrequick it will give a quick understanding about regex.

    Sample of replicating your error:

    #!/usr/bin/perl use strict; use warnings; my $dir = shift // '.'; opendir my $dh, $dir or die "Could not open '$dir' for reading '$!'\n"; my @things = grep {$_ ne '.' and $_ ne '..'} readdir $dh; foreach my $thing (@things) { if(my $pattern =~ /^test/){ print $thing . "\n"; } } closedir $dh or die "Could not close dir hanlde '$dh': '$!'\n"; __END__ Monks$ perl test.pl Use of uninitialized value $pattern in pattern match (m//) at test.pl +line 11. Use of uninitialized value $pattern in pattern match (m//) at test.pl +line 11.

    I would also recommend to refactor a bit your code an use readdir .

    Sample from my localhost with no error:

    #!/usr/bin/perl use strict; use warnings; my $dir = shift // '.'; opendir my $dh, $dir or die "Could not open '$dir' for reading '$!'\n"; my @things = grep {$_ ne '.' and $_ ne '..'} readdir $dh; foreach my $thing (@things) { if($thing =~ /^test/){ print $thing . "\n"; } } closedir $dh or die "Could not close dir hanlde '$dh': '$!'\n"; __END__ Monks$ perl test.pl test.pl~ test.pl
    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2019-12-07 19:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (162 votes). Check out past polls.

    Notices?