Cantidad variable de parámetros

Cantidad variable de parámetros

Postby Antonio Linares » Thu Nov 27, 2014 8:29 am

Cuando necesiteis llamar a una función y proporcionarle un número variable de parámetros podeis usar:

function MyFunc( ... )

y entonces llamar a hb_aparams() para obtener un array con todos ellos :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41394
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Cantidad variable de parámetros

Postby fgondi » Thu Nov 27, 2014 10:22 am

Muchas gracias por la información, Antonio
Un saludo
Fernando González Diez
ALSIS Sistemas Informáticos
User avatar
fgondi
 
Posts: 694
Joined: Fri Oct 07, 2005 6:58 am
Location: Palencia, España

Re: Cantidad variable de parámetros

Postby Carlos Mora » Thu Nov 27, 2014 5:36 pm

Antonio,

En general no es imprescindible indicar ni siquiera que parámetros pasas, a menos que quieras ponerle un nombre ;) , pero si vas a usar hb_aparams, o HB_PValue(), ni siquiera hace falta indicarlo con '...' .

HB_PValue(i) devuelve el parámetro i-ésimo, y sabremos cuantos parámetros hay con PCount().

Por ejemplo:

? Expand( 'Select * from clientes where id = $1', 22 ) -> Select * from clientes where id = 22

FUNCTION Expand( cQuery ) // Aunque escribiendo (cQuery, ... ) hace más obvio que tiene parámetros variables
LOCAL i
i:= PCount()
WHILE i > 1
cQuery:= StrTran( cQuery, '$'+(LTrim(Str(i-1))), ClipValue2SQL( HB_PValue(i) ) )
i--
ENDDO

RETURN cQuery

Se pueden hacer cosas bastante majas...
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Carlos Mora
 
Posts: 988
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: Cantidad variable de parámetros

Postby Antonio Linares » Thu Nov 27, 2014 5:59 pm

Carlos,

gracias por la información :-)

Dudo que Przemek pasará por alto algo asi. Tal vez haya otro uso para "..." que desconocemos.

Si os parece importante lo podemos preguntar en la lista de desarrollo de Harbour
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41394
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Cantidad variable de parámetros

Postby Carlos Mora » Fri Nov 28, 2014 8:53 am

Solo se me ocurre una cuestion de orden y elegancia, una forma de hacer explícito el hecho de que vas a usar parámetros.
Tal vez el pcode generado podría llegar a mostrar alguna diferencia, no se...

Pero en lo personal lo he usado bastante, como en el ejemplo, y como podrás adivinar, con uso intensivo (es en las querys) y va fenomenal.

Y para añadir más cosas a la colección de cositas guapas, por si alguno todavía no lo conoce, van los siguientes #pragmas:

Code: Select all  Expand view

#xcommand TEXT INTO <v> => #pragma __cstream|<v>:=%s
#xcommand TEXT INTO <v> ADDITIVE => #pragma __cstream|<v>+=%s
 


Esto nos permite escribir texto literal, que incluya incluso saltos de línea, como en:

Code: Select all  Expand view

   TEXT INTO cTexto

      CREATE TABLE IF NOT EXISTS `validalta` (
        `id`         int(11) NOT NULL AUTO_INCREMENT,
        `fechanaci`  date,
        `nif`        varchar(9)  DEFAULT NULL,
        `mail`       varchar(50) DEFAULT NULL,
        `cuenta`     varchar(4) DEFAULT NULL,
        PRIMARY KEY (`id`),
        UNIQUE KEY `NIF` (`nif`)
      );
   ENDTEXT

   oServer:Command( cTexto )
 


o bien, XML combinado con la funcioncita de Expand

Code: Select all  Expand view

               TEXT INTO cTransac
                  <tns:importAbsenceFiles xmlns:tns="http://echange.service.open.server.com">
                     <tns:absenceFilesToImport>
                        <tns:absenceFile>
                           <tns:absenceTypeAbbreviation>VACAC</tns:absenceTypeAbbreviation>
                           <tns:employeeIdentificationNumber>$1</tns:employeeIdentificationNumber>
                           <tns:startDate>$2</tns:startDate>
                           <tns:endDate>$3</tns:endDate>
                        </tns:absenceFile>
                     </tns:absenceFilesToImport>
                  </tns:importAbsenceFiles>
               ENDTEXT

               cTransac:= Expand( cTransac, oQuery:dni, oQuery:FechaInicio, oQuery:FechaFin )

 


Como te decía, entre SQL, XML y web... lo uso de forma intensiva, y ayuda a hacer la programación muchísimo más clara.
Last edited by Carlos Mora on Fri Nov 28, 2014 11:10 pm, edited 1 time in total.
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Carlos Mora
 
Posts: 988
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: Cantidad variable de parámetros

Postby Antonio Linares » Fri Nov 28, 2014 2:40 pm

Carlos,

Muy bueno :-)

Muchas gracias por compartirlo :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41394
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Cantidad variable de parámetros

Postby Carles » Fri Nov 28, 2014 9:19 pm

Carlos --> Bravo, bonissimo para mi, no lo conocia.... :lol:

(Copy/paste cagando leches....)


Gracias !!!
Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
Skype -> https://join.skype.com/cnzQg3Kr1dnk
User avatar
Carles
 
Posts: 1104
Joined: Fri Feb 10, 2006 2:34 pm
Location: Barcelona

Re: Cantidad variable de parámetros

Postby csincuir » Fri Nov 28, 2014 9:46 pm

Cada día de aprende algo nuevo!
Gracias tocayo, muy bueno, me serán de bastante utilidad tus ejemplos.

Saludos.

Carlos.
csincuir
 
Posts: 397
Joined: Sat Feb 03, 2007 6:36 am
Location: Guatemala

Re: Cantidad variable de parámetros

Postby Baxajaun » Sat Nov 29, 2014 8:37 am

Carlos,

muchísimas gracias por compartir la información y los ejemplos.

Saludos
User avatar
Baxajaun
 
Posts: 962
Joined: Wed Oct 19, 2005 2:17 pm
Location: Gatika. Bizkaia

Re: Cantidad variable de parámetros

Postby cnavarro » Sat Nov 29, 2014 10:35 am

Carlos
Gracias, es muy bueno
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6504
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Cantidad variable de parámetros

Postby AngelSalom » Sat Nov 29, 2014 12:50 pm

Uau, uau, uau .. esos #pragma son la repera !! :D :D
Angel Salom
Visionwin Software - https://www.visionwin.com
------------------------------------------------------------
fwh 19.05 - harbour 3.2 - bcc 7.4
User avatar
AngelSalom
 
Posts: 708
Joined: Fri Oct 07, 2005 7:38 am
Location: Benicarló (Castellón ) - España

Re: Cantidad variable de parámetros

Postby fgondi » Mon Dec 01, 2014 9:18 am

Carlos, muy, pero que muy bueno

Fácil de leer en el código fuente, facil de copiar y pegar para probar la consulta sql.

Increíble.
Un saludo
Fernando González Diez
ALSIS Sistemas Informáticos
User avatar
fgondi
 
Posts: 694
Joined: Fri Oct 07, 2005 6:58 am
Location: Palencia, España

Re: Cantidad variable de parámetros

Postby Antonio Linares » Mon Dec 01, 2014 9:34 am

Si os parece incluimos esos pragmas en FiveWin.ch :-)

Controlando que sea Harbour, porque imagino que no funcionan en xHarbour
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41394
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Cantidad variable de parámetros

Postby Antonio Linares » Mon Dec 01, 2014 9:35 am

Code: Select all  Expand view
#ifndef __XHARBOUR__
   #xcommand TEXT INTO <v> => #pragma __cstream|<v>:=%s
   #xcommand TEXT INTO <v> ADDITIVE => #pragma __cstream|<v>+=%s
#endif
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41394
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Cantidad variable de parámetros

Postby carlos vargas » Mon Dec 01, 2014 3:00 pm

Antonio, no se como lo maneja xharbour internamente, pero segun la ayuda
Syntax
TEXT [TO PRINTER] [TO FILE <fileName>]
<text>
ENDTEXT

or

TEXT INTO <varName>
<text>
ENDTEXT

Arguments
TO PRINTER
The text is additionally output to the printer.
TO FILE <fileName>
The text is additionally output to the file <fileName>. It can be specified as a literal file name or as a character expression enclosed in parentheses. The default extension is TXT.
INTO <varName>
This is the symbolic name of the variable to assign <text> to.
<text>
This is a literal text block enclosed in TEXT and ENDTEXT. The text is output to the screen and displayed exactly as written in the PRG source code file. Description
The TEXT...ENDTEXT command outputs a block of literal text to the console. Output can be directed additionally to a printer or a file. When SET CONSOLE is set to OFF, the screen output is suppressed.
ALternatively, the text is assigned as character string to the variable <varName> when the INTO option is used.
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1688
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Next

Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 34 guests