in reply to Need help with loop syntax errors

I made a few corrections for you below. "diagnostics" just gives a more verbose explanation of error messages. I don't use it.
#!/usr/bin/perl # Demonstrate different loops for use with string substitutions use strict; use warnings; # use diagnostics; my $NewRev = "\tabc\tkis17.1.33en_12345.exe<h href"; my $RedirectUrl; s/.*kis17/17/, s/.exe//, s/en_/./ for $NewRev; #you assigned null to $ +NewRev print "for at the end: \$NewRev = <$NewRev>\n\n"; $NewRev = "\tabc\tkis17.1.33en_12345.exe<h href"; #set back to origina +l $RedirectUrl = $NewRev =~ s/.*kis17/17/r =~ s/.exe//r =~ s/en_/./r ; print "run on line: \$RedirectUrl = <$RedirectUrl>\n"; #modified print "run on line: \$NewRev = <$NewRev>\n\n"; #not modified $NewRev = "\tabc\tkis17.1.33en_12345.exe<h href"; #set back to origina +l for ($NewRev) {s/.*kis17/17/; s/.exe//; s/en_//; } print "for at the start: \$NewRev = <$NewRev>\n\n"; $NewRev = "\tabc\tkis17.1.33en_12345.exe<h href"; #set back to origina +l 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__ Need __END__ not just END But of course __END__ is optional, just use it like this when you want to put something in the code like an example printout Prints: for at the end: $NewRev = <17.1.33.12345<h href> run on line: $RedirectUrl = <17.1.33.12345<h href> run on line: $NewRev = < abc kis17.1.33en_12345.exe<h href> for at the start: $NewRev = <17.1.3312345<h href> for at the start: $NewRev = <17.1.3312345<h href>

Replies are listed 'Best First'.
Re^2: Need help with loop syntax errors
by Todd Chester (Scribe) on Sep 13, 2016 at 01:48 UTC

    Thank you!

    I am going to chew on this until I understand it and then post it back. Sometimes and example is worth a thousand words!

    What do you mean "you assigned null to $NewRev"?

      You had
      my $NewRev = "\tabc\tkis17.1.33en_12345.exe<h href"; my $RedirectUrl; ## and then... for($NewRev = $RedirectUrl);
      That assigns $RedirectUrl to $NewRev and then uses that result in the for loop. Since $RedirectUrl is null undefined (the simple my $RedirectUrl; statement). That is the source of one of your "use of uninitiatized value warnings". Note that the code keeps going after a warning. A warning is not considered "fatal". An "error" will stop the program.

      Make Sense?

      Update: I don't want to quibble about definitions of definitions of "warning, syntax error" or whatever. You have some code that runs now without any warnings. Continue on with your experimentation...I commend you for that effort.

        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>