Diferencia entre revisiones de «Programación en Perl/Capítulo 2.5»

Contenido eliminado Contenido añadido
Esmarger (discusión | contribs.)
New page: ==Cadenas o Strings== ===Introduction to Strings=== String es cadena en inglés. En programación una cadena es un tipo de dato. En concreto las secuencias de caracteres. Como por ejempl...
 
Esmarger (discusión | contribs.)
Sin resumen de edición
Línea 13:
====Double Quoted Strings====
 
Cuando usas " estas diciendo que interprete la cadena de caracteres, por lo que algunos de estos CAMBIARÁN. Ya sé que es confuso pero un ejemplo te suele ayudar a entender.
When you use " to represent a string, you are saying that you want some certain values in a string to be changed to other values. This may sound a little confusing, but this example should help you understand:
 
"Hello¡Hola WorldMundo!\n"
 
el OUTPUT sería
Notice that if you printed this string in a program, the output would not be "Hello World!\n", it would be Hello World! and a newline. This is because when you are using double quotes, the <tt>\n</tt> character is interpreted as what is called a ''backslash escape''. Here are some backslash escapes:
 
¡Hola Mundo!
*<tt>'''\n'''</tt> - Newline
*<tt>'''\t'''</tt> - Tab
*<tt>'''\b'''</tt> - Backspace
*<tt>'''\l'''</tt> - Lowercase Next Letter
*<tt>'''\u'''</tt> - Uppercase Next Letter
*<tt>'''\a'''</tt> - Ring System Bell
 
¿Por que no aparece este trozo \n?. Perl lo ha INTERPRETADO como un ENTER del teclado. \n es un backslash escape, que no tengo ni idea de como traducirlo, en Perl hay mas de estos:
 
*<tt>'''\n'''</tt> - ENTER también llamado nueva línea
These are just some of the escapes that you can use in double quoted strings. When you learn about variables and math operations in the next couple of chapters, you will see that the values of variables can also be automatically inserted into strings. For now, however, let's move on to single quoted strings.
*<tt>'''\nt'''</tt> - NewlineTabulador
*<tt>'''\b'''</tt> - Backspace no se muy bien traducir
*<tt>'''\u'''</tt> - Haz mayúscula la siguiente letra. Siendo estas letras mayusculas(ATYUKNHV) y estas minúsculas (mnreobjsd).
*<tt>'''\l'''</tt> - Haz minúscula la siguiente letra
*<tt>'''\a'''</tt> - Ház un ring con la campana del sistema, que realmente es un pitidito típico de ordenador.
 
====Single Quoted Strings====
 
Cuando usas <tt>'</tt> para rodear a la cadena. Estas comillitas son mśa DEMOCRÁTICAS ya que tratan a todos los caracteres por igual, sin distinción, asi que ves EXACTAMENTE lo que escribes.
Single quoted strings are represented by using <tt>'</tt> to enclose the string. Unlike a double quoted string, a single quoted string expresses exactly what you type in. In single quoted strings, you cannot use the backslash escapes, and values of variables will not be automatically substituted. Here's an example:
 
#!/usr/bin/perl
Línea 39 ⟶ 40:
print 'Hello World!\n';
 
El OUTPUT sería:
This little program is very much like your first program except for the quotes used in the string to be printed. This makes a huge difference. Instead of printing "Hello World!" and a newline, this modified version will print "Hello World!\n". As you can see, what you type is interpreted literally.
 
Hello World!\n
If you need to use a single quote inside a string, you can do so by using <tt>\'</tt>. For example:
 
Sin un ENTER cuando perl hace el print.
 
¿Y como haces para imprimir una comilla simple en mitad de una cadena si te interesa? dirá algún espabilado, pués para no defraudar PErl tiene esta magnifica herramienta, la barra \ si la pones antes de cualquier caracter, Perl no la interpretará (aunque esté entre comillas donles ")
 
print 'Those are Mark\'s keys';
 
el OUTPUT sería:
 
Those are Mark's keys
 
===String Operators===
 
Operadores en matemáticas sino recuerdo mal son + - * osea suma, resta , multiplicación ese tipo de cosas y es algo muy muy similar a lo que hacen con cadenas de caracteres o strings.
''Operators'' manipulate two or more strings in some way.
 
''Operadores'' Manipulan una o más cadenas a la vez.
 
 
====The <tt>.</tt> Operator====
 
El operdaor <tt>.</tt> UNE las cadenas.
The <tt>.</tt> operator is used to connect strings like this:
 
"Hello" . "World" # ThisEs isequivalente the same asa "HelloWorld"
 
Puedes hacer esto "Hello World" (que tiene un espacio entre las palabras) asi de complicado:
If you want to make the string have a space between Hello and World you could write it like this:
 
"Hello" . " " . "World" # ThisMenuda istontéz the, same as "Hello World"¿verdad?
 
Ahora pones el espacio pegado al World.
Or like this:
 
"Hello" . " World" # ThisEste istambién the same ases "Hello World"
 
====The <tt>x</tt> Operator====
 
Conocido como ''el repetidor de cadenas''. Se usa poniendo 1ºla cadena, 2º una x y 3º el nº de repeticiones. Asi: "eo" x 23 :
This is called the ''string repetition'' operator and is used to repeat a string. All you have to do is put a string on the left side of the <tt>x</tt> and a number on the right side. Like this:
 
"Hello" x 5 # This is the same as "HelloHelloHelloHelloHello"
 
"Hello" x 5 # ThisEs isequivalente the same asa "HelloHelloHelloHelloHello"
If you wish to insert a line break after each output of the string, use:
 
SI deseas hacer unos cuantos ENTERS puedes hacerlo así:
"Hello\n" x 5
 
"Hello\n" x 5
===Exercises===
 
===Ejercicios===
*Write a program that uses the <tt>.</tt> operator to print "Hello Sir!".
 
*Haz un programa que se el operador <tt>.</tt> y que imprima muchos puntos (.) por ejemplo "..---... son morse"
*Write another program which uses the <tt>x</tt> operator to print "HelloHelloHelloHello". Put comments in this program that explain how it works
 
*¡Programa! es la UNICA manera de aprender, copia el código de est e wiki, modificálo , juega con él.
*Remember to take some time to play with single and double quoted strings, the more practice you get, the better you will be.
 
{{prognav|Perl|Basic Variables|Numbers}}