@ $% - What does this mean?!

February 4, 2009 · Posted in Perl

Before anything, I would like to thank Blabos the invitation and the introduction of so exaggerated ... :)

Well, I'm here to talk about perl, so let's start.

Variables such as the name implies, they are used to store variable values. In perl there are three basic types of variables (pay attention, I said basic). These are:

  • Scalar
  • Array
  • Hash

They serve w / store values. Each of these "types" are represented with a symbol. When you refer to a scalar value, you must use the symbol "$" to array "@" and hash "%".

Let's see this in practice:

Scalar

To create a scalar variable called "var_scalar", you simply use it:

  $ Var_scalar; 

Pretty simple, is not it? Now, as I put a value into this variable? Even easier:

  0 ; Var_scalar $ = 0; 

Nothing more logical, right? Only you use this variable, perl already creates w / you ... Cool huh? So that's how I seto a numeric value to a scalar in perl, but what if I want to use a string? Well, w / just this:

  "String setada dentro da variável" ; Var_scalar $ = "String setada into the variable"; 

Yes, you got it right! You need not say whether it will be number or text! For Perl is "indifferent"!

This're pretty easy, huh? Let's move on to the next case, Array.

Arrays

The array is the symbol for "@". So how would a valid name for an array? How about this?

  @ Var_array; 

This! But what exactly is an array? Array 'a variable that contains a list of values. For example:

  ( 1 , 2 , 3 , 4 , 5 ) ; @ Array_numerico = (1, 2, 3, 4, 5);
 ( "lalala" , "lelele" , "lilili" , "lololo" , "lululu" ) ; Array_de_strings = @ ("lalala", "lelele", "lilili", "lololo", "lululu");
 ( 1 , "lalala" , 2 , "lelele" , 3 , "lilili" , 4 ) ; @ Array_misturado = (1, "lalala", 2, "lelele," 3, "lilili", 4); 

Good ... But in the first case, the @ array_numerico if I wanted to populate the array with numbers from 1 to 100, I'd have to write the numbers one to one? No, there is easy way:

  ( 1 ... 100 ) ; @ Array_numerico = (1 ... 100); 

Pretty simple to understand, much less to write the numbers 1 through 100 ...

Well, we understand how to set a whole array at once ... But if I want to create value for value?! Is it possible? Sure!

  0 ] = "Posição 0..." ; $ Meu_array [0] = "0 Position ...";
 1 ] = "Posição 1..." ; $ Meu_array [1] = "a position ...";
 2 ] = "Posição 2..." ; $ Meu_array [2] = "2 Position ...";
 3 ] = "Posição 3..." ; $ Meu_array [3] = "3 Position ...";
 4 ] = "Posição 4..." ; $ Meu_array [4] = "Position 4 ..."; 

Pera where I now confused! You did not say that was the symbol of the array "@"?! Because you're using "$"?!

Well, the brackets ("[]") indicate that we are looking for a position in an array, and in this position have nothing more than a scalar value! So we use the "$". If we were using more than one position in the array (a list of values), we would use the "@".

  0 , 2 , 4 ] = ( "Posição 0..." , "Posição 2..." , "Posição 4..." ) ; @ Meu_array [0, 2, 4] = ("Position 0 ...", "Position 2 ...", "Position 4 ..."); 

That yes, we are now dealing with a list of values, just use the "@" ...

Array - A set of functions

Arrays in Perl has a very specific set of functions to them, let's see some:

  • push
  • shift
  • pop
  • unshift
  • splice

Let's start the push includes one (or more) value (s) in an array, for example:

  , "posição 0" ; # agora o array contém "posição 0". push @ array_exemplo, "0 position"; # ​​array now contains "0 position".
 , 1 , 2 , 3 ; # agora é ("posição 0", 1, 2, 3) push @ array_exemplo, 1, 2, 3, is now # ("position 0", 1, 2, 3)
 , "pos 4" ; # e agora ("posição 0", 1, 2, 3, "pos 4") push @ array_exemplo, "4 post"; # now ("position 0", 1, 2, 3, "four post") 

Got it?

The shift removes and returns the first element of the array, let's see how it works:

  ; # o array agora tem (1, 2, 3, "pos 4") shift @ array_exemplo; # The array now has (1, 2, 3, "four post")
 shift @array_exemplo ; # array:(2, 3, "pos 4"), scalar: 1 = Shift @ $ meu_scalar array_exemplo; # array: (2, 3, "4 post"), scalar: 1
 shift @array_exemplo ; # array:(3, "pos 4"), scalar: 2 = Shift @ $ meu_scalar array_exemplo; # array: (3, "4 post"), scalar: 2 

Right? The pop removes and returns the last element of the array and unshift incui one or more elements at the beginning of the array.

The syntax of the splice is seginte:

  , $posicao , $quantidade , "lista" , "de" , "elementos" ; splice @ array, $ position, $ quantity, "list", "a", "elements"; 

Well, let's see some examples:

 ( 1 ... 10 ) ; # (1 ,2, 3, 4, 5, 6, 7, 8, 9, 10) splice @array , 5 , 2 ; # (1, 2, 3, 4, 5, 8, 9, 10) # Eu não passei uma lista p/ botar no lugar, então # ele foi na posição 5 do array (no valor 6) e subs- # tituiu os 2 elementos apartir daquela posição (5 e 6) # pelos elementos da lista (nada) splice @array , 2 , 1 , "lalala" , "lelele" ; # (1, 2, "lalala", "lelele", 4, 5, 8, 9, 10) # ele foi na posição 2, e substituiu 1 elemento apartir # dessa posição pelos elementos da lista ("lalala", "lelele") splice @array , 6 , 0 , "teste" ; # (1, 2, "lalala", "lelele", 4, 5, "teste", 8, 9, 10) # vai na posição 6, subistitua nenhum elemento pela lista = @ Array (1 ... 10), # (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) @ array splice, 5, 2, # (1, 2, 3, 4 , 5, 8, 9, 10) # I have not spent a list w / put in place, then he was the # 5 position in the array (the value 6) and replaces the tituiu # 2 elements starting from that position (5 and 6 ) by elements of the list # (nothing) splice @ array, 2, 1, "lalala", "lelele", # (1, 2, "lalala", "lelele", 4, 5, 8, 9, 10) # he was in position 2, and replaced an element of # starting position by elements of the list ("lalala", "lelele") splice @ array, 6, 0, "test", # (1, 2, "lalala", " lelele ", 4, 5, 'test', 8, 9, 10) goes into position # 6, substitute any element from the list 

Besides these functions, the arrays have some other interesting things ...

  ( 1 ... 5 ) ; @ Array = (1 ... 5);
 , " \n " ; # imprime 5 print scalar @ array, "\ n"; # ​​prints 5
 , " \n " ; # imprime 4 print $ # array, "\ n"; # ​​print 4 

How so?! Scalar?! this is a variable?! okay but without the "$"! $ # array?! what does this mean?!

Calm down ... I'll explain! No, not a scalar variable, the scalar changes the context of the expression p / scalar context (as the name says ...) and an array in scalar context returns the number of such elements. In this case, the array containing a 5 ... means that it contains five elements. And the $ # array? Well, every array that you create, creates this "variable". If the name of your array is @ array, the variable $ # array will be, if the name is @ list, will be $ # @ list and if estou_sem_criatividade_pra_inventar_nome_de_array, it will be $ # estou_sem_criatividade_pra_inventar_nome_de_array. And what does that do? This returns the last position of the array. If an array has five elements, and the indices start from 0, so the last position of the array is 4.

Hashes

Simply array! Let's see the hashes. The symbol w / hash is the "%". Hash is a list of values, different from the arrays are not indexed by numbers but by names, and are not ordered.

  Hash_exemplo%;
 ( "Janeiro" => 1 , "Fevereiro" => 2 , "Março" => 3 ) ; Meu_hash% = ("Jan" => 1, 'February' => 2, "Mar" => 3); 

The% meu_hash has three positions, the position "January", the "February" and "March". The position "January" contains the value 1, "February" and contains two "March" contains three.

If you want to write position by position, you can also:

  "Abril" } = 4 ; Meu_hash $ {"April"} = 4;
 "Maio" } = 5 ; Meu_hash $ {"May"} = 5;
 "Junho" } = 6 ; Meu_hash $ {"June"} = 6;
 "Julho" } = 7 ; Meu_hash $ {"July"} = 7; 

The braces ("{}") indicate that it is a position of hash, and "$" indicates that this position is a scalar value.

Hashes - Functions

The hash function is also interesting:

  • keys
  • values
  • delete

The keys returns all keys name (positions) of the hash:

  keys %meu_hash ; # @chaves contem ("Janeiro", "Fevereiro", @ Keys = keys% meu_hash; # @ keys contains ("January", "February",
 # "March", "April", "May", "June", "July") 

The values ​​returns the value of all positions:

  values %meu_hash ; # @valores contém (1, 2, 3, 4, 5, 6, 7) Values ​​@ = values ​​meu_hash%; # @ contains values ​​(1, 2, 3, 4, 5, 6, 7) 

And delete delete a position and returns the hash value of that position:

  delete $meu_hash { "Maio" } ; # o hash não tem mais posição maio May $ delete = $ meu_hash {"May"}, # hash position is no longer in May
 May contain # and $ 5. 

Well, I'd better stop by here ... I think I ended up sweeping and it was great ...

Thanks for reading. If this is very easy w / you, you might want a look at the perl-e.org , where the material is for advanced programmers.

Are you interested? want to know more?

Blabos Again, thanks for the space, and until next time!

Comments

  • http://blabos.org blabos

    Very good text. Congratulations!

  • http://perl-e.org SmokeMachine

    Thank you

  • http://br-linux.org/2009/mini-tutorial-sobre-perl/ Mini Tutorial on Perl

    [...] By the Blabos Blebe (blabosΘblabos · org) - reference [...]

  • garu

    Congratulations! Very well explained, great introduction to variables in Perl

  • Sicka

    Uia! I thought it was word = P

    Jokes aside, I really liked your text. I look forward to the next.

  • Mage

    I found this confusing:

    Meu_hash% = ("Jan" => 1, 'February' => 2, "Mar" => 3);

    I think the best examples would be:

    Meu_hash% = ('January' => 28,
    'February' => 35,
    'March' => 29);

    Meu_hash% = ("January", 1, "February", 2, "March", 3);

  • http://estilotabi.blogspot.com Tairone

    What is Pearl?

    She creates programs for Linux?

  • http://blabos.org blabos

    Perl and not 'Pearl' is a programming language (you can create programs with it) multiplatform (runs on linux too).

    Be careful not to be confused with the language Pearl, which is another programming language, to have nothing to do with Perl from the post.

    Take a look at the links at the end of post ;)

    Hugs

  • http://perl-e.org SmokeMachine

    Perhaps it would be interesting to give you a read here: http://rio.pm.org/faq.pl

  • http://blog.blabos.org/2009/02/escrevendo-na-tela/ Writing on the screen: Blog Blabos of Blebe

    [...] The quotes, he says that what is to come is a scalar variable (as discussed in my last post) then if we use the backslash () before that character, he loses all his specialty, [. ..]

  • Junius

    Thank you for your contribution, is of great value. Peace and health to you!

blog comments powered by Disqus