in reply to Re^3: Need help with loop syntax errors
in thread Need help with loop syntax errors

ooops. Now it is clear. And I got it all working. Thank you!

#!/usr/bin/perl # Demonstrate different loops for use with string substitutions use strict; use warnings; # use diagnostics; my $RevStr = "\tabc\tkis17.1.33en_12345.exe<h href"; my $NewRev; print "Original \$RevStr = <$RevStr>\n"; $NewRev = $RevStr; s/.*kis17/17/, s/.exe//, s/en_/./, s/\<h.*// for $NewRev; print "for at the end: \$NewRev = <$NewRev>\n"; $NewRev = $RevStr; $NewRev = $RevStr =~ s/.*kis17/17/r =~ s/.exe.*//r =~ s/en_/./r; print "run on line: \$NewRev = <$NewRev>\n"; #modified $NewRev = $RevStr; for ($NewRev) {s/.*kis17/17/; s/.exe.*//; s/en_/./; s/\<h.*//; } print "for at the start: \$NewRev = <$NewRev>\n"; $NewRev = $RevStr; for ($NewRev) #white spaces consumes no Mips! why so compact? { s/.*kis17/17/; #all 3 statements modify $NewRev s/.exe.*//; s/en_/./; } print "for at the start: \$NewRev = <$NewRev>\n\n"; __END__

./LeftToRight.pl
Original $RevStr = < abc kis17.1.33en_12345.exe<h href>
for at the end: $NewRev = <17.1.33.12345>
run on line: $NewRev = <17.1.33.12345>
for at the start: $NewRev = <17.1.33.12345>
for at the start: $NewRev = <17.1.33.12345>

Replies are listed 'Best First'.
Re^5: Need help with loop syntax errors
by Marshall (Canon) on Sep 13, 2016 at 03:42 UTC
    Hi Todd!

    Glad to help and I'm happy that you have all the versions working to your satisfaction.

    It is obvious that I like the last version because I added it to your test code. So now comes some of my opinion as opposed to "fact"... Why did I do that?

    As I commented, white space doesn't consume any CPU cycles but it can be some of the most important "code" that you write. Just imagine if there wasn't a blank line between the 4 different "test" cases? That would make the source code a lot more difficult to understand! The computer doesn't care about white space, but humans do!

    In the first case, I have to read through a bunch of substitutions before I finally come to the thing that is being modified at the very end of the line! In this case, I probably want to know that $NewRev is being modified first, not last. Usually, I read the details later if I care (and maybe I don't even care). On the first read of new code, I just want to get the general picture of what is going on. There are situations where this syntax and use of the comma operator makes sense. I just don't think that this is one of those cases.

    I think the next couple of versions are "ok". But I like the last version best in this particular situation. What is being modified just "jumps out". It is easy for me to glance at the three statements that do the mods to get the idea of what they do or skip over them if I don't really care at the moment. By having a line for each regex, I can put a comment there, like perhaps # backward compatible, re bug #Q45983.

    I know that a "style post" like this can generate controversy. I don't want to get into an extended discussion. I do encourage you to consider the issue and form your own opinion. "Compact" code does not necessarily equate to "fast" or "clear" code.