Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This is really more a question of aesthetics and coding practice than whether it works or not. Should "my" just be thrown into the middle of the code, where it's nearest to where the variable is needed, or stated at the top, where it's easier to see all your variables and where the "my" doesn't cause things to potentially not line up? For instance, if you were doing something like this:

my $saved = pop @array; print "Now we have @array and I have saved $saved\n"; $saved = pop @array; print "Now we have @array and I have saved $saved\n";

Personally, I like declaring at the top, especially if a variable is used several times, but others prefer declaring at the point at which the variable is first used. I still haven't figured out which method is really superior.

Here's a few more samples of interesting things you can do with arrays:

Using the standard output variable instead of declaring a new one:

$_ = pop @arr; print "Value popped is $_\n";

Cycling through the array, method 1 (each value is put in standard output variable in turn):
print "Value is $_\n" for @arr;

Cycling through the array, method 2 ($# represents the subscript of the last item in the array):
print "Value is $arr[$_]\n" for 0..$#arr;


In reply to Re: Is it ok to assign a popped value to a variable as shown. by TJPride
in thread Is it ok to assign a popped value to a variable as shown. by perl514

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found