Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Need help with loop syntax errors

by Todd Chester (Scribe)
on Sep 13, 2016 at 00:42 UTC ( [id://1171609]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,

I am trying to write myself a demonstration of differenct types of word subsitutions in two differnt types of loops. Problem: I can't figure out all the syntax errors. Any help would be greatly approciated.

#!/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 = $RedirectUrl; print "for at the end: \$RedirectUrl = <$RedirectUrl>\n\n"; $NewRev = $RedirectUrl =~ s/.*kis17/17/r =~ s/.exe//r =~ s/en_/./r ; print "run on line: \$RedirectUrl = <$RedirectUrl>\n\n"; for ($NewRev = $RedirectUrl) {s/.*kis17/17/; s/.exe//; s/en_//; } print "for at the start: \$RedirectUrl = <$RedirectUrl>\n\n"; END

Use of uninitialized value $_ in substitution (s///) at ./LeftToRight.pl line 12.
Use of uninitialized value $_ in substitution (s///) at ./LeftToRight.pl line 12.
Use of uninitialized value $_ in substitution (s///) at ./LeftToRight.pl line 12.
Use of uninitialized value $RedirectUrl in concatenation (.) or string at ./LeftToRight.pl line 13.
for at the end: $RedirectUrl = <>

Use of uninitialized value $RedirectUrl in substitution (s///) at ./LeftToRight.pl line 15.
Use of uninitialized value $RedirectUrl in substitution (s///) at ./LeftToRight.pl line 15.
Use of uninitialized value $RedirectUrl in substitution (s///) at ./LeftToRight.pl line 15.
Use of uninitialized value $RedirectUrl in concatenation (.) or string at ./LeftToRight.pl line 16.
run on line: $RedirectUrl = <>

Use of uninitialized value $_ in substitution (s///) at ./LeftToRight.pl line 18.
Use of uninitialized value $_ in substitution (s///) at ./LeftToRight.pl line 18.
Use of uninitialized value $_ in substitution (s///) at ./LeftToRight.pl line 18.
Use of uninitialized value $RedirectUrl in concatenation (.) or string at ./LeftToRight.pl line 19.
for at the start: $RedirectUrl = <>

Replies are listed 'Best First'.
Re: Need help with loop syntax errors
by Marshall (Canon) on Sep 13, 2016 at 01:42 UTC
    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>

      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.

Re: Need help with loop syntax errors (warnings)
by Anonymous Monk on Sep 13, 2016 at 00:50 UTC

    warnings are warnings not syntax errors, if they're unclear, use diagnostics;

    also, it doesn't make sense to keep writing  $NewRev = $RedirectUrl

      Thanks for the tip on the wording. This is what I thought I was using: "In computer science, the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language."
      Wikipedia Syntax
      I will come up to speed eventually!

      I uncommented out "use diagnostics". I don't mean to be dense, but, I still can't figure out what I am doing wrong? What is the correct structure?

      $ ./LeftToRight.pl Use of uninitialized value $_ in substitution (s///) at ./LeftToRight. +pl line 12 (#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl will try to tell y +ou the name of the variable (if any) that was undefined. In some cas +es it cannot do this, so it also tells you what operation you used th +e undefined value in. Note, however, that perl optimizes your progr +am anid the operation displayed in the warning may not necessarily ap +pear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program. Use of uninitialized value $RedirectUrl in concatenation (.) or string + at ./LeftToRight.pl line 13 (#1) for at the end: $RedirectUrl = <> Use of uninitialized value $RedirectUrl in substitution (s///) at ./LeftToRight.pl line 15 (#1) Use of uninitialized value $RedirectUrl in concatenation (.) or string + at ./LeftToRight.pl line 16 (#1) run on line: $RedirectUrl = <> Use of uninitialized value $_ in substitution (s///) at ./LeftToRight. +pl line 18 (#1) Use of uninitialized value $RedirectUrl in concatenation (.) or string + at ./LeftToRight.pl line 19 (#1) for at the start: $RedirectUrl = <>

        blah blah blah

        There is no problem with the structure, there is no syntax problem, no syntax error, like I said, the warning you get is a warning, its not a syntax error, and its telling you what the problem is, and diagnostics telling you in more than one line

        What its saying is that $RedirectUrl is uninitialized, it doesn't have a value, its empty, its undef, its value is undef, there is nothing thiere, its an empty bucket full of space, you wont find find en_ or .exe or anything in the bucket, its empty, there is nothing inside to find but air, you can keep searching the empty bucket, it will continue to be empty,

        perl warned you, and you're like, man perl is telling me this bucket is empty, how am I supposed to hold the bucket?

        an empty bucket is empty, if you want it to have stuff inside, put stuff in it, dont put empty in it

        Maybe you'd like to stop writing   $NewRev = $RedirectUrl ?

      I keep "$NewRev" as I am trying to demonstrate several different methods of doing the same thing. Erase and reuse.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found