in reply to What does [=;] mean

Please forgive me changing the question but I have worked some of it out.

The main issue is in the part of the code
foreach my $i (keys %spl) { print "$i:$spl{$i}\n"; }
Could you please explain to me what is happening with this code ?
I understand the array contains PROGEEK3GEEKS2GFG1
I understand the colon is associated with describing a hash
the out put is:

PROGEEK:3
GEEKS:2
GFG:1

I dont understand
how does the code know to use the numbers in the scalar as keys for the hash ?

thankyou

Replies are listed 'Best First'.
Re^2: What does [=;] mean
by syphilis (Archbishop) on Nov 27, 2019 at 23:09 UTC
    how does the code know to use the numbers in the scalar as keys for the hash ?

    Actually, the numbers are the values.
    The array is (GFG, 1, GEEKS, 2, PROGEEK, 3).
    The keys will always be the the first, third, fifth, seventh, ... items of the array.
    The values will always be the second , fourth, sixth, eighth, ... items, and each of those values will be associated with the key that precedes it.
    my %spl = ('GFG', '1', 'GEEKS', '2', 'PROGEEK', '3'); foreach my $key (keys %spl) { print "$key:$spl{$key}\n"; }
    Note that the keys are not necessarily printed out in the same order. For a first run, I get:
    GEEKS:2 GFG:1 PROGEEK:3
    A second run yields:
    PROGEEK:3 GEEKS:2 GFG:1
    and a third run produces:
    GFG:1 GEEKS:2 PROGEEK:3
    But the key-value association remains unchanged.

    Cheers,
    Rob
Re^2: What does [=;] mean
by GrandFather (Saint) on Nov 27, 2019 at 23:19 UTC

    First off, I cringe every time I see PERL. So don't do that. Most often you mean 'Perl', the language (as opposed to 'perl' the interpreter).

    In Perl a % at the front of something means the thing returns a hash. A hash is kinda like an array except that it contains key => value pairs. The split built the hash contents by generating an array of paired entries.

    The for loop iterates over the keys for the hash %spl. Note that the keys are 'PROGEEK', 'GEEKS' and 'GFG'. The values happen to be numbers - they could be strings or other things.

    Perl has a really nifty feature called 'interpolation' which replaces variables in a string with the content of the variable. The print uses interpolation in its string to print the key (in $i) followed by a colon (just boring text - no magic here) then the value in the %spl hash associated with the key in $i (that's the $spl{$i} bit).

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Re PERL, my aplogies :)
      Thankyou I am working through this now
Re^2:What does [=;] mean
by harangzsolt33 (Deacon) on Nov 28, 2019 at 02:01 UTC

    foreach my $i (keys %spl)
    {
        print "$i:$spl{$i}\n";
    }

    The foreach keyword is a loop. Usually we write:

          foreach XXX (YYY) { ... }

    where YYY is an array or hash. The foreach loop will loop through all elements of the array or hash, and each time it will allow you to access the value of the current element by mentioning XXX. So, XXX is going to be a scalar. So, I should really spell it as $XXX. If you omit $XXX, then you can access the element through $_

    Inside the foreach brackets, you can refer to $_ or $XXX, and it will hold the value of an element of your array or hash.

    You know, a hash is basically a simple array whose values are used as "pointers" to refer to string values. We can think of them as key-value pairs. So, if you use a foreach loop, then the $_ or scalar $XXX is going to hold the key. And you can get the value that it refers to by writing $HASH_NAME{$_} or $HASH_NAME{$XXX}

    For example, the environmental variables are stored in a hash in perl. You can access them by reading $ENV{key}. In the following little example, we sort the hashes by key and then we print the key-value pairs:

    foreach (sort keys %ENV)
    {
        print "$_ : $ENV{$_} \n";
    }

    We could also write it like this :

    foreach my $i (sort keys %ENV)
    {
        print "$i : $ENV{$i} \n";
    }

    Here is another simple example. This one creates an array that holds the values: ("Happy" "New" "Year!!" "2020" "is" "here") and then it replaces each one with the word "happy":

    my @G = qw(Happy New Year!! 2020 is here); print "\n $G[0]"; print "\n $G[1]"; print "\n $G[2]"; print "\n $G[3]"; print "\n $G[4]"; print "\n $G[5]"; print "\n-----------"; foreach (@G) { $_ = 'happy'; } print "\n $G[0]"; print "\n $G[1]"; print "\n $G[2]"; print "\n $G[3]"; print "\n $G[4]"; print "\n $G[5]";