the square brackets in the regular expression in split's first argument denotes that a match should be made for each of the characters within the brackets. So, split(/[=;]/, $has); splits the contents of string $has whenever it sees a = or a ;.

The result is an array: ('GFG', '1', 'GEEKS', '2','PROGEEK', '3')

But you insist on creating a hash from split's output when you write (note: %spl): my %spl = split(/[=;]/, $has);. That's not a problem for Perl. It will create a hash for you. And you know a hash consists of (key,value) pairs. So your array's odd-indexed elements are the keys and even-indexed elements are the values (assuming zero-based arrays like Perl's) even-indexed elements (e.g. 0,2,4) are your KEYS and odd-indexed elements (e.g. 1,3,5) are your values. And so your hash is (CFG => 1, GEEKS=>2, PROGEEK=>3) (hash notation is (key => value, ...))

So, now you have the hash you wanted, though you created it using a shortcut. I say "shortcut" because the logic in creating that hash from that string, in another language say Java, would be to mark your position, then look for a =, that would be the key. Then mark your position and look for a ;. That would be your value. Insert into hash and repeat till the end of string. But Perl allows you the shortcut of converting an even-size array to a hash automatically. And that is what you do because split returns back an array. And your my %spl coerces that array into a hash.

The last part of the code is printing that hash. And, being Perl, it speaks for itself. You seem confused by print "$i:$spl{$i}\n";. It just says to print the key ($i) and then print a colon and then print the value ($spl{$i}).

The example code you cited should have concentrated either on split or on hashes but (the year being 2019 and money has to be made from anything...) it makes a mess of it by mixing concepts. More confusion = more hits = more advertisments. But that's my inference. In any event, thanks Knuth Perlmonks exists.

bw, bliako


In reply to Re: What does [=;] mean by bliako
in thread What does [=;] mean by Scotmonk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.