Can you explain the Perl hash system to a PHP guy?

How do Perl hashes work? Are they like arrays in PHP or completely different animals?

From what I understand, is this all an associative array? This is what I thought until I started talking to the Perl programmer, who told me that I was completely wrong, but could not explain it in any way that did not make me look.

Anyway, the way I thought it worked is as follows

PHP:

$argv['dog_name'] = 'missy'; $argv[0] = 'tree'; 

same as Perl:

 my %argv{'dog_name'} = 'missy'; my $argv[0] = 'tree'; 

Right? But you cannot print (% argv {'dog_name'}) , you need (return?) To print ($ argv {'dog_name'}) , which is confusing?

Now he is trying to print as a variable, as in PHP, echo $ argv ['dog_name']; ? Does this (again) mean that the hash is just an associative PHP array with% to declare, but $ to access?

I don’t know, I hope that PHP / Perl Guru can explain how hashes work and how similar they are for PHP arrays.

+4
source share
8 answers

To write

 $argv['dog_name'] = 'missy'; $argv[0] = 'tree'; 

in Perl, you should write it like this:

 $argv{dog_name} = 'missy'; $argv{0} = 'tree'; 

if you have strict conditions that you need, then you need to provide a variable:

 my %argv; $argv{dog_name} = 'missy'; $argv{0} = 'tree'; 

If the above is a little repeated for you, you can write it:

 my %argv = ( dog_name => 'missy', 0 => 'tree', ); 

More information can be found on the perldata manpage.

In short, the reasons for changing sigils from % to $ are that %hash refers to a multiple hash (a list of key value pairs), and $hash{foo} refers to a single hash element. The same thing happens with arrays, where @ refers to a full array, and $ refers to a single element. (for both arrays and hashes, the leading @ sigil with an index means a data slice where several keys are transmitted and a list of values ​​is returned)

+11
source

To get a little understanding of Ambrose's answer, the reason for your confusion is the difference between the philosophy of using siglings in Perl and PHP.

In PHP, the sigil is tied to an identifier. For instance. the hash identifier will ALWAYS have a hash symbol around it.

In Perl, the sigil is tied to how you access the data structure (you get access to 1 value, a list of values, or a whole hash of values) - see other great answers like Eric for details.

+5
source

%argv{'dog_name'} is a syntax error. You need $argv{'dog_name'} .

But you're right that the perl hash is just an associative array (why perl chose a different terminology, I don't know).

For a complete understanding of hashes, I recommend reading any of the many perl tutorials or books that cover this topic. Perl programming is a great choice, or here is a random online tutorial I also found.

+3
source

I, like Flimsi, would also recommend Perl programming. As recent PHP for Perl converts itself, it taught me a lot of language.

The % character is used to create a complete "associative array", as we might think about it. For example, I could create an associative array by doing the following:

 %hash = ('key1' => 'value1', 'key2' => 'value2'); 

Then I could print it like this:

 print %hash; 

The result will be something like this:

 'key2value2key1value1' 

This, I believe, is called a "list context" because % indicates that we are talking about a range of values.

On the other hand, if I wanted to access a single value, we would have to use the $ signal. This, as the Perl programmer tells us, can be thought of as an “S” for “Scalar”. We must use the $ sign whenever it comes to a singular value.

So, in order to access a single element in an array, I would have to use the following syntax:

 print $hash{'key1'}; 

The same goes for arrays. A full array can be created as follows:

 @array = ('abc', '123'); 

and then type like this:

 print @array; 

But to access one element of the array, I would instead type:

 print $array[0]; 

There are many basic principles. You should familiarize yourself with the "list context" and the "scalar context" in detail. Soon, you'll also want to take a look at the links you use to create multidimensional structures in Perl. I would recommend "Perl Programming"! It was hard to read in chapters, but it certainly covers everything you need to know (and much more).

+2
source

Changing the sigil is really not as difficult as you make it sound. You already do this in English without thinking about it.

If you have a set of cars, then you will talk about “these cars” (or “those cars”). It is like an array.

 my @cars = ('Vauxhall', 'Ford', 'Rolls Royce'); 

If you are talking only about one car from this set, you switch to using this "car". It looks like a single element from an array.

 say $car[1]; # prints 'Ford'; 

Similar rules apply to hashes.

+2
source

"Sigil", that is, the character before the variable name, indicates the amount of data accessed, as follows:

  • If you say $hash{key} , you are using a scalar context, ie one value.

  • For the plural or list context, sigma changes to @ , so @hash{('key1', 'key2')} returns a list of two values ​​associated with the two keys, respectively (can also be written as @hash{qw(key1 key2)} ).

  • %hash used to access the hash as a whole.

The same applies to arrays: $arr[0] = 1 , but @arr[1 .. 10] = (10) x 10 .

+1
source

I would say that your confusion is partly due to one simple fact. Perl has different sigils for different things. PHP has one sigil for everything.

So, if you put something in an array / hash or get something or declare a simple scalar variable, in PHP you always use the dollar sign.

With perl, you need to be more specific to everything.

+1
source

I hope you do not expect to get a complete perl hash guide. You don't need a Perl guru to explain hashes to you, just a simple google search.

http://www.perl.com/pub/2006/11/02/all-about-hashes.html

PS: please increase the reception ratio - 62% is quite low

0
source

Source: https://habr.com/ru/post/1384425/


All Articles