Solved

Simple label question

Posted on 2000-04-10
7
150 Views
Last Modified: 2010-03-05
I need to jump from within the middle of the statement to another part of the program.
Please remind me how to use a label to jump to another place.
Thank You.
0
Comment
Question by:sstouk
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2700833
Following is an extract from the PERL book "Learning PERL"
================================================
Labeled Blocks

What if you want to jump out of the block that contains the innermost block, or to put it another way, exit from two nested
blocks at once? In C, you'd resort to that much maligned goto to get you out. No such kludge is required in Perl; you can use
last, next, and redo on any enclosing block by giving the block a name with a label.

A label is yet another type of name from yet another namespace following the same rules as scalars, arrays, hashes, and
subroutines. As we'll see, however, a label doesn't have a special prefix punctuation character (like $ for scalars, & for
subroutines, and so on), so a label named print conflicts with the reserved word print and would not be allowed. For this
reason, you should choose labels that consist entirely of uppercase letters and digits, which will never be chosen for a reserved
word in the future. Besides, using all uppercase stands out better within the text of a mostly lowercase program.

Once you've chosen your label, place it immediately in front of the statement containing the block followed by a colon, like this:

SOMELABEL: while (condition) {
    statement;
    statement;
    statement;
    if (nuthercondition) {
        last SOMELABEL;
    }
}

We added SOMELABEL as a parameter to last. This tells Perl to exit the block named SOMELABEL, rather than just the
innermost block. In this case, we don't have anything but the innermost block. But suppose we had nested loops:

OUTER: for ($i = 1; $i <= 10; $i++) {
    INNER: for ($j = 1; $j <= 10; $j++) {
        if ($i * $j == 63) {
            print "$i times $j is 63!\n";
            last OUTER;
            }
        if ($j >= $i) {
            next OUTER;
        }
    }
}

This set of statements tries all successive values of two small numbers multiplied together until it finds a pair whose product is
63 (7 and 9). Once the pair is found, there's no point in testing other numbers, so the first if statement exits both for loops
using last with a label. The second if ensures that the bigger of the two numbers will always be the first one by skipping to
the next iteration of the outer loop as soon as the condition would no longer hold. This means that the numbers will be tested
with ($i, $j) being (1,1), (2,1), (2,2), (3,1), (3,2), (3,3), (4,1), and so on.

Even if the innermost block is labeled, the last, next, and redo statements without the optional parameter (the label) still
operate with respect to that innermost block. Also, you can't use labels to jump into a block - just out of a block. The last,
next, or redo has to be within the block.
0
 
LVL 6

Author Comment

by:sstouk
ID: 2700866
I don't know why but it doesn't jump to the label.
if (CHECK_EMAIL($USERMAIL)) # sub 19
                  {
                        if ($PASSWORD eq $PASSCHECK)
                        {


                        print "all is correct";
                        JUMP1_LABEL;
        };

# ==============
# Some other code
# ==============

JUMP1_LABEL : print 'It Jumped!';


This code doesn't jump to the label.

0
 
LVL 6

Author Comment

by:sstouk
ID: 2700880
# =======================
# Simple example
# =======================

print 'Before jump';

JUMP;

print 'after jump';

JUMP: print 'it jumped!';

# =======================
# end of "Simple example";
# =======================



The above code just exits and does print only 'Before Jump';
and nothing else.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Accepted Solution

by:
maneshr earned 20 total points
ID: 2700898
try this...

basically i have modified your simple example.
what this does is prints Before jump and then goes to the JUMP label and prints it jumped and exits out of the program as there is nothing more to do after that

#!/usr/local/bin/perl

print "Before jump\n";

goto JUMP;

print "after jump\n";

JUMP: print "it jumped!\n";
0
 
LVL 6

Author Comment

by:sstouk
ID: 2700901
I think it recognizes the label "JUMP" as the function which doesn't exists.

Do I have to declare the label before using it?
0
 
LVL 6

Author Comment

by:sstouk
ID: 2700910
Thank You very much.

You havn't only modified it, but You have added the "goto" statement.
This is what it needed and I completely forgot about it.

Thanks alot!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2700932
most welcome!! :-)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question