mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 01:51:38 +00:00
awk-es script trig funcs
This commit is contained in:
parent
fed2f2a19e
commit
915a4b21f7
@ -83,19 +83,19 @@ BEGIN {
|
|||||||
for (i = 0; i < 10; i++)
|
for (i = 0; i < 10; i++)
|
||||||
print "El viejo confiable ciclo for"
|
print "El viejo confiable ciclo for"
|
||||||
|
|
||||||
# As for comparisons, they're the standards:
|
# Los operaciones de comparación son estándar...
|
||||||
a < b # Less than
|
a < b # Menor que
|
||||||
a <= b # Less than or equal
|
a <= b # Menor o igual que
|
||||||
a != b # Not equal
|
a != b # No igual
|
||||||
a == b # Equal
|
a == b # Igual
|
||||||
a > b # Greater than
|
a > b # Mayor que
|
||||||
a >= b # Greater than or equal
|
a >= b # Mayor o igual que
|
||||||
|
|
||||||
# Logical operators as well
|
# ...así como los operadores lógicos
|
||||||
a && b # AND
|
a && b # AND
|
||||||
a || b # OR
|
a || b # OR
|
||||||
|
|
||||||
# In addition, there's the super useful regular expression match
|
# Además están las expresiones regulares
|
||||||
if ("foo" ~ "^fo+$")
|
if ("foo" ~ "^fo+$")
|
||||||
print "Fooey!"
|
print "Fooey!"
|
||||||
if ("boo" !~ "^fo+$")
|
if ("boo" !~ "^fo+$")
|
||||||
@ -104,83 +104,85 @@ BEGIN {
|
|||||||
# Arrays
|
# Arrays
|
||||||
arr[0] = "foo"
|
arr[0] = "foo"
|
||||||
arr[1] = "bar"
|
arr[1] = "bar"
|
||||||
# Unfortunately, there is no other way to initialize an array. Ya just
|
# Desafortunadamente no hay otra manera de inicializar un array.
|
||||||
# gotta chug through every value line by line like that.
|
# Tienes que inicializar cada posición del array.
|
||||||
|
|
||||||
# You also have associative arrays
|
# También hay arrays asociativos
|
||||||
assoc["foo"] = "bar"
|
assoc["foo"] = "bar"
|
||||||
assoc["bar"] = "baz"
|
assoc["bar"] = "baz"
|
||||||
|
|
||||||
# And multi-dimensional arrays, with some limitations I won't mention here
|
# Y arrays multidimensionales con limitaciones que no mencionaré aquí
|
||||||
multidim[0,0] = "foo"
|
multidim[0,0] = "foo"
|
||||||
multidim[0,1] = "bar"
|
multidim[0,1] = "bar"
|
||||||
multidim[1,0] = "baz"
|
multidim[1,0] = "baz"
|
||||||
multidim[1,1] = "boo"
|
multidim[1,1] = "boo"
|
||||||
|
|
||||||
# You can test for array membership
|
# Puedes probar pertenencia a un array
|
||||||
if ("foo" in assoc)
|
if ("foo" in assoc)
|
||||||
print "Fooey!"
|
print "Fooey!"
|
||||||
|
|
||||||
# You can also use the 'in' operator to traverse the keys of an array
|
# También puedes usar el operador 'in' para iterar las claves de un array
|
||||||
for (key in assoc)
|
for (key in assoc)
|
||||||
print assoc[key]
|
print assoc[key]
|
||||||
|
|
||||||
# The command line is in a special array called ARGV
|
# La terminal es un array especial llamado ARGV
|
||||||
for (argnum in ARGV)
|
for (argnum in ARGV)
|
||||||
print ARGV[argnum]
|
print ARGV[argnum]
|
||||||
|
|
||||||
# You can remove elements of an array
|
# Puedes eliminar elementos de un array.
|
||||||
# This is particularly useful to prevent AWK from assuming the arguments
|
# Esto es útil para prevenir que AWK suponga que algunos argumentos
|
||||||
# are files for it to process
|
# son archivos por procesar.
|
||||||
delete ARGV[1]
|
delete ARGV[1]
|
||||||
|
|
||||||
# The number of command line arguments is in a variable called ARGC
|
# El número de argumentos de la terminal está en la variable ARGC
|
||||||
print ARGC
|
print ARGC
|
||||||
|
|
||||||
# AWK has several built-in functions. They fall into three categories. I'll
|
# AWK tiene tres categorías de funciones incluidas.
|
||||||
# demonstrate each of them in their own functions, defined later.
|
# Demostraré esas funciones posteriormente.
|
||||||
|
|
||||||
return_value = arithmetic_functions(a, b, c)
|
return_value = arithmetic_functions(a, b, c)
|
||||||
string_functions()
|
string_functions()
|
||||||
io_functions()
|
io_functions()
|
||||||
}
|
}
|
||||||
|
|
||||||
# Here's how you define a function
|
# Así se define una función
|
||||||
function arithmetic_functions(a, b, c, localvar) {
|
function arithmetic_functions(a, b, c, localvar) {
|
||||||
|
|
||||||
# Probably the most annoying part of AWK is that there are no local
|
# Probablemente la parte más molesta de AWK es que no hay variables locales
|
||||||
# variables. Everything is global. For short scripts, this is fine, even
|
# Todo es global. No es problema en scripts pequeños, pero sí para
|
||||||
# useful, but for longer scripts, this can be a problem.
|
# scripts más grandes.
|
||||||
|
|
||||||
# There is a work-around (ahem, hack). Function arguments are local to the
|
# Hay un work-around (mmm... hack). Los argumentos de las funciones son
|
||||||
# function, and AWK allows you to define more function arguments than it
|
# locales para la función, y AWK permite definir más argumentos de función
|
||||||
# needs. So just stick local variable in the function declaration, like I
|
# de los que necesita, por lo que define las variables locales en la
|
||||||
# did above. As a convention, stick in some extra whitespace to distinguish
|
# declaración como en la función de arriba. Como convención, agrega
|
||||||
# between actual function parameters and local variables. In this example,
|
# espacios en blanco para distinguir los parámetros de la función de las
|
||||||
# a, b, and c are actual parameters, while d is merely a local variable.
|
# variables locales. En este ejemplo, a, b y c son parámetros y localvar es una
|
||||||
|
# variable local.
|
||||||
|
|
||||||
# Now, to demonstrate the arithmetic functions
|
# Ahora, a demostrar las funciones aritméticas
|
||||||
|
|
||||||
# Most AWK implementations have some standard trig functions
|
# La mayoría de las implementaciones de AWK tienen funciones
|
||||||
|
# trigonométricas estándar
|
||||||
localvar = sin(a)
|
localvar = sin(a)
|
||||||
localvar = cos(a)
|
localvar = cos(a)
|
||||||
localvar = atan2(a, b) # arc tangent of b / a
|
localvar = atan2(a, b) # arcotangente de b / a
|
||||||
|
|
||||||
# And logarithmic stuff
|
# Y cosas logarítmicas
|
||||||
localvar = exp(a)
|
localvar = exp(a)
|
||||||
localvar = log(a)
|
localvar = log(a)
|
||||||
|
|
||||||
# Square root
|
# Raíz cuadrada
|
||||||
localvar = sqrt(a)
|
localvar = sqrt(a)
|
||||||
|
|
||||||
# Truncate floating point to integer
|
# Trucar un flotante a entero
|
||||||
localvar = int(5.34) # localvar => 5
|
localvar = int(5.34) # localvar => 5
|
||||||
|
|
||||||
# Random numbers
|
# Números aleatorios
|
||||||
srand() # Supply a seed as an argument. By default, it uses the time of day
|
srand() # La semilla es el argumento. Por defecto usa el tiempo del sistema
|
||||||
localvar = rand() # Random number between 0 and 1.
|
localvar = rand() # Número aleatorio entre 0 y 1.
|
||||||
|
|
||||||
# Here's how to return a value
|
# Y aquí se regresa el valor
|
||||||
return localvar
|
return localvar
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user