toolman Posted November 19, 2013 Share Posted November 19, 2013 Hi there, I am trying to fix a bug in a Perl script. Can someone explain what the below code does/would do? while (my $a = readdir(DIR)){ if($a =~ m/(.+?)_(air)?port/ && $a !~ m/featured_(air)?port/ && $a !~ m/london_airport/i && $a !~ m/glasgow/ && $a !~ m/_opt/i){ push(@dirs,$a); } } Thanks Quote Link to comment Share on other sites More sharing options...
Irate Posted November 19, 2013 Share Posted November 19, 2013 Your human-readable code would be something like this. "While we can still read files from this directory, loop through them; then, if it matches the expression m/(.+?)_(air)?port/ but does not match the other ones, push the current file into the array 'dirs'." Quote Link to comment Share on other sites More sharing options...
dalecosp Posted November 19, 2013 Share Posted November 19, 2013 Bingo ... you beat me. Although the last part should probably say "push the name of the current directory into the array 'dirs'". Quote Link to comment Share on other sites More sharing options...
toolman Posted November 19, 2013 Author Share Posted November 19, 2013 Thanks. What does the m/(.+?) part mean as there are also other m/ m/(.+?)_(air)?port/ also, does the script mean if the files are broken/missing, it will revert to london_airport? I'm not sure if that makes sense. Thanks Quote Link to comment Share on other sites More sharing options...
dalecosp Posted November 19, 2013 Share Posted November 19, 2013 (edited) m/ is "multiline mode". https://courses.cs.washington.edu/courses/cse190m/12sp/cheat-sheets/php-regex-cheat-sheet.pdf also, does the script mean if the files are broken/missing, it will revert to london_airport? Not sure what you mean, either. It will create a list (array) of dirnames that have to do with airports, but not featured airports or London airport, or Glasgow or any dir with "opt" in it, more or less. Edited November 19, 2013 by dalecosp Quote Link to comment Share on other sites More sharing options...
Irate Posted November 19, 2013 Share Posted November 19, 2013 Those expressions with m/(.+?)_/ and others are regular expressions. The prefix m is for multiline mode, as dalecops pointed out previously. There are many guides to understand regular expressions, but they are pretty complex subjects and even though I've been trying to for the past months, I haven't completely understood everything about them. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.