Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Quick easy question: What does $i mean?

by sicone (Initiate)
on Sep 23, 2005 at 20:42 UTC ( [id://494665]=perlquestion: print w/replies, xml ) Need Help??

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

What does $i mean I can't even find it on google! thanks

2005-09-24 Retitled by g0n, as per Monastery guidelines
Original title: 'Quick easy question'

  • Comment on Quick easy question: What does $i mean?

Replies are listed 'Best First'.
Re: Quick easy question: What does $i mean?
by kirbyk (Friar) on Sep 23, 2005 at 20:53 UTC
    Additionally, it's a long-standing tradition (from early versions of Fortran, if I know my history correctly) to name loop control variables starting with $i (and $j and $k for nested loops.)
    It's generally considered highly bad form to have such undescriptive variable names, but small loops (less than 10 lines) tend to not be so bad to figure it out, and it's idiomatic. Everyone has always used $i for that, since the Dawn Of Time, before you had as much control over what to name your variables.
    But it's not a special perl variable at all. You could change it to $loop_counter everywhere, and it'd work just fine.

    -- Kirby, WhitePages.com

      It goes back to mathematics, which often uses i, j, k for indexes in summations and arrays, matrices, and such...

        Actually it goes back to Fortran where variables starting with the letters i-n (INteger) are integer and (I think all) others are real by default.


        Perl is Huffman encoded by design.
Re: Quick easy question: What does $i mean?
by philcrow (Priest) on Sep 23, 2005 at 20:47 UTC
    Loosely speaking, in Perl variables are either singular or plural. Singular variables, which we call scalars, store one thing. To keep track of what is singular and what is plural, we use funny characters in front of the variable names. $ is the funny character for scalar. So $i stores one thing, like a single number or a string of text. (Funny characters are usually called sigils.)

    Phil

Re: Quick easy question: What does $i mean?
by chester (Hermit) on Sep 23, 2005 at 20:51 UTC
    perlintro has a basic description of Perl syntax and variable types.
Re: Quick easy question: What does $i mean?
by GrandFather (Saint) on Sep 23, 2005 at 20:51 UTC

    Well, yes, that is a quick and easy question. I don't think there is a quick and easy answer because that depends on what you actually want to know and you've not given enough context for us to tell. Here are a couple of answers to get you started:

    • It is a scalar variable which may be used to store a number or a string or a reference to something
    • In a context like this for (my $i = 0; $i < $limit; ++$i) is is a counter or, more likely, an index in a C'ish for loop. There are probably better ways to do this.

    Perl is Huffman encoded by design.
      I'm curious about the second bullet item in your reply. Is this the "better way" you were thinking of?
      for (0 .. $limit) { #... }
      What's wrong with the C-ish for loop?

      I'm always looking for a better mousetrap!
        What's wrong with the C-ish for loop?

        It's harder to read. The index variable appears three times, and the upper bound is expressed as a logical expression, yet we think of the bound as a number. With a Perl-ish for loop, the index variable only appears once, and the bounds are expressed as numbers, just like in our mind.

        To support my case, let me point out you made an error reading the bounds of the C-ish version:

        for (0 .. $limit-1) { #... } ^^ was missing
        cause we like the perl-ish way better :) But i think the point is that in perl, the array (assuming that's the context) indices themselves aren't needed. For example,
        for (my $i = 0; $i < $#array + 1; ++$i){ print $array[$i] . "\n"; }
        Is "better" written:
        foreach my $s ( @array ){ print "$s\n"; } # OR one of many other ways like: print $_."\n" for @array;
        Of course, if the output needs the array index in it, that changes things...
Re: Quick easy question: What does $i mean?
by sauoq (Abbot) on Sep 23, 2005 at 21:16 UTC

    I'm guessing you have a ways to go before you finish that MUD you want to write... I think someone should recommend Learning Perl.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Quick easy question: What does $i mean?
by sicone (Initiate) on Sep 23, 2005 at 21:36 UTC
    Thanks, I believe that it is the loop name here is a specific example
    my ($i); for ($i = 0; ($i < int (@commandsProperOrder)); $i += 2) { $commandsProperOrder{$commandsProperOrder[$i]}= $commandsProperOrder[$i + 1]; }
    this code is full of $i's
      First off, you'll want to learn how to post here. If you put your code between "<code>" and "</code>" tags, then normal code punctuation is escaped for you. So this:
      my ($i); for ($i = 0; ($i < int (@commandsProperOrder)); $i += 2) { $commandsProperOrder{$commandsProperOrder$i} = $commandsProperOrder$i + 1; }
      looks like this:
      my ($i); for ($i = 0; ($i < int (@commandsProperOrder)); $i += 2) { $commandsProperOrder{$commandsProperOrder[$i]} = $commandsProperOrder[$i + 1]; }
      (OK, I had to break some lines because of the long variable names, but otherwise it came out as typed.)

      Second, I'd guess that the array @commands has a list of pairs, and that the code is trying to put those pairs into the hash %commands. Normally you want to know (or check) that your array is appropriate for this, but hash stuffing is more easily done with:

      %commands = @commands;
      No $is, etc., just Perl magic.

      This works because a list can be assigned to a hash, and it will (sometimes) do the right thing. (It's certainly no worse than the code you posted.) [When you're ready, just ask, someone will tell you when and why it doesn't do the right thing.]

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found