• Publicidad

Problema con hashes

¿Apenas comienzas con Perl? En este foro podrás encontrar y hacer preguntas básicas de Perl con respuestas aptas a tu nivel.

Problema con hashes

Notapor BigBear » 2012-10-13 14:17 @636

Estoy haciendo el siguiente script.

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!usr/bin/perl
  2. #Radio X
  3. #Version 0.2
  4. #Coded By Doddy H
  5. #
  6. #Download : http://www.mplayerhq.hu/MPlayer/releases/win32/MPlayer-mingw32-1.0rc2.zip
  7. #
  8.  
  9. use Cwd;
  10.  
  11. my %emisoras = (
  12.  
  13. 0 => {
  14.  
  15. "nombre" => "Absolute Classic Rock (Broadband)",
  16. "genero" => "Rock'n'Roll",
  17. "link" => "http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vcbb"
  18.  
  19. },
  20.  
  21. 1=> {
  22.  
  23. "nombre" => "Absolute Classic Rock (Modem)",
  24. "genero" => "Rock'n'Roll",
  25. "link" => "http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vc"
  26.  
  27. },
  28.  
  29. 2=> {
  30.  
  31. "nombre"=>"Absolute Radio (Broadband)",
  32. "genero"=>"Pop",
  33. "link"=>"http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vrbb"
  34.  
  35. },
  36.  
  37. 3=>{
  38.  
  39. "nombre"=>"Absolute Radio (Modem)",
  40. "genero"=>"Pop",
  41. "link"=>"http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vr"
  42.  
  43. },
  44.  
  45. 4=>{
  46.  
  47. "nombre"=>"Absolute Xtreme (Broadband)",
  48. "genero"=>"Modern Rock",
  49. "link"=>"http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vxbb"
  50.  
  51. },
  52.  
  53. 5=>{
  54.  
  55. "nombre"=>"Absolute Xtreme (Modem)",
  56. "genero"=>"Modern Rock",
  57. "link"=>"http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vx"
  58.  
  59. },
  60.  
  61. 6=>{
  62.  
  63. "nombre"=>"HBR1.com - Dream Factory",
  64. "genero"=>"Ambient",
  65. "link"=>"http://ubuntu.hbr1.com:19800/ambient.ogg"
  66.  
  67. },
  68.  
  69. 7=>{
  70.  
  71. "nombre"=>"HBR1.com - I.D.M. Tranceponder",
  72. "genero"=>"Trance",
  73. "link"=>"http://ubuntu.hbr1.com:19800/trance.ogg"
  74.  
  75. },
  76.  
  77. 8=>{
  78.  
  79. "nombre"=>"HBR1.com - Tronic Lounge",
  80. "genero"=>"House",
  81. "link"=>"http://ubuntu.hbr1.com:19800/tronic.ogg"
  82.  
  83. },
  84.  
  85. 9=>{
  86.  
  87. "nombre"=>"NRK Alltid Klassisk (Norway)",
  88. "genero"=>"Desconocido",
  89. "link"=>"http://media.hiof.no/streams/m3u/nrk-alltid-klassisk-172.ogg.m3u"
  90.  
  91. },
  92.  
  93. 10=>{
  94.  
  95. "nombre"=>"NRK Alltid Nyheter (Norway)",
  96. "genero"=>"Desconocido",
  97. "link"=>"http://media.hiof.no/streams/m3u/nrk-alltid-nyheter-172.ogg.m3u"
  98.  
  99. },
  100.  
  101. 11=>{
  102.  
  103. "nombre"=>"NRK mP3 (Norway)",
  104. "genero"=>"Desconocido",
  105. "link"=>"http://media.hiof.no/streams/m3u/nrk-mpetre-172.ogg.m3u"
  106.  
  107. },
  108.  
  109. 12=>{
  110.  
  111. "nombre"=>"NRK P1 (Norway)",
  112. "genero"=>"General",
  113. "link"=>"http://media.hiof.no/streams/m3u/nrk-p1-172.ogg.m3u",
  114.  
  115. },
  116.  
  117. 13=>{
  118.  
  119. "nombre"=>"NRK P2 (Norway)",
  120. "genero"=>"General",
  121. "link"=>"http://media.hiof.no/streams/m3u/nrk-p2-172.ogg.m3u"
  122.  
  123. },
  124.  
  125. 14=>{
  126.  
  127. "nombre"=>"NRK P3 (Norway)",
  128. "genero"=>"General",
  129. "link"=>"http://media.hiof.no/streams/m3u/nrk-petre-172.ogg.m3u"
  130.  
  131. },
  132.  
  133. 15=>{
  134.  
  135. "nombre"=>"WKNC 88.1 FM (NC State) (High Quality)",
  136. "genero"=>"Music",
  137. "link"=>"http://wknc.sma.ncsu.edu:8000/wknchq.ogg.m3u"
  138.  
  139. },
  140.  
  141. 16=>{
  142.  
  143. "nombre"=>"WKNC 88.1 FM (NC State) (Low Quality)",
  144. "genero"=>"Music",
  145. "link"=>"http://wknc.sma.ncsu.edu:8000/wkncmq.ogg.m3u"
  146.  
  147. }
  148.  
  149. );
  150.  
  151. head();
  152.  
  153. for my $em(keys %emisoras) {
  154.  
  155. print "\n[+] Number : ".$em."\n";
  156. print "[+] Name : ".$emisoras{$em}{nombre}."\n";
  157. print "[+] Station : ".$emisoras{$em}{genero}."\n";
  158.  
  159. #print $emisoras{$em}{nombre}."\n";
  160. }
  161.  
  162. sub head {
  163.  
  164. print qq(
  165.  
  166.  
  167.  @@@@@     @    @@@@    @   @@@@     @     @
  168.  @    @    @    @   @   @  @    @    @     @
  169.  @    @   @ @   @    @  @  @    @     @   @
  170.  @    @   @ @   @    @  @  @    @      @ @  
  171.  @@@@@   @   @  @    @  @  @    @       @  
  172.  @    @  @   @  @    @  @  @    @      @ @  
  173.  @    @  @@@@@  @    @  @  @    @     @   @
  174.  @    @ @     @ @   @   @  @    @    @     @
  175.  @    @ @     @ @@@@    @   @@@@     @     @
  176.  
  177.  
  178.  
  179. );
  180.  
  181. }
  182.  
  183. sub copyright {
  184. print "\n\n-- == Doddy Hackman 2012 == --\n\n";
  185. <stdin>;
  186. exit(1);
  187. }
  188.  
  189. #The End ?  
Coloreado en 0.004 segundos, usando GeSHi 1.0.8.4


El problema es que necesito que el lugar de los hashes no se desordenen porque los necesito de 0 a 16.

¿ Alguien conoce alguna forma ?
BigBear
Perlero frecuente
Perlero frecuente
 
Mensajes: 981
Registrado: 2009-03-01 18:39 @818

Publicidad

Re: Problema con hashes

Notapor explorer » 2012-10-13 15:08 @672

1.- Usa un array.

2.- Usa un sort() numérico a la hora de recuperar las claves. Hummm... sí: es mejor usar un array.
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Problema con hashes

Notapor BigBear » 2012-10-13 16:06 @713

¡Qué desgracia! Justo cuando quería ahorrar líneas. El código quedó así:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!usr/bin/perl
  2. #Radio X
  3. #Version 0.2
  4. #Coded By Doddy H
  5. #
  6. #Download : http://www.mplayerhq.hu/MPlayer/release ... 1.0rc2.zip
  7. #
  8.  
  9. use Cwd;
  10.  
  11. my @emisoras =("Absolute Classic Rock (Broadband)","Absolute Classic Rock (Modem)","Absolute Radio (Broadband)","Absolute Radio (Modem)","Absolute Xtreme (Broadband)","Absolute
  12.  
  13. Xtreme (Modem)","HBR1.com - Dream Factory","HBR1.com - I.D.M. Tranceponder","HBR1.com - Tronic Lounge","NRK Alltid Klassisk (Norway)","NRK Alltid Nyheter (Norway)","NRK mP3
  14.  
  15. (Norway)","NRK P1 (Norway)","NRK P2 (Norway)","NRK P3 (Norway)","WKNC 88.1 FM (NC State) (High Quality)","WKNC 88.1 FM (NC State) (Low Quality)");
  16.  
  17. my @generos = ("Rock'n'Roll","Rock'n'Roll","Pop","Pop","Modern Rock","Modern
  18.  
  19. Rock","Ambient","Trance","House","Desconocido","Desconocido","Desconocido","General","General","General","Music","Music");
  20.  
  21. my @links = ("http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vcbb","http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?
  22.  
  23. service=vc","http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vrbb","http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?
  24.  
  25. service=vr","http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vxbb","http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?
  26.  
  27. service=vx","http://ubuntu.hbr1.com:19800/ambient.ogg","http://ubuntu.hbr1.com:19800/trance.ogg","http://ubuntu.hbr1.com:19800/tronic.ogg","http://media.hiof.no/streams/m3u/nrk-
  28.  
  29. alltid-klassisk-172.ogg.m3u","http://media.hiof.no/streams/m3u/nrk-alltid-nyheter-172.ogg.m3u","http://media.hiof.no/streams/m3u/nrk-mpetre-
  30.  
  31. 172.ogg.m3u","http://media.hiof.no/streams/m3u/nrk-p1-172.ogg.m3u","http://media.hiof.no/streams/m3u/nrk-p2-172.ogg.m3u","http://media.hiof.no/streams/m3u/nrk-petre-
  32.  
  33. 172.ogg.m3u","http://wknc.sma.ncsu.edu:8000/wknchq.ogg.m3u","http://wknc.sma.ncsu.edu:8000/wkncmq.ogg.m3u");
  34.  
  35. $SIG{INT} = \&menu;  
  36.  
  37. menu();
  38.  
  39. sub menu {
  40.  
  41. head();
  42.  
  43. my $total = int(@links) - 1;
  44.  
  45. for my $em(0..$total) {
  46.  
  47. print "\n[+] Number : ".$em."\n";
  48. print "[+] Name : ".$emisoras[$em]."\n";
  49. print "[+] Station : ".$generos[$em]."\n";
  50.  
  51. }
  52.  
  53. print "\n\n[+] Option : ";
  54. chomp(my $op = <stdin>);
  55.  
  56. chdir(getcwd()."/mplayer/");
  57.  
  58. system("mplayer $links[$op]");
  59.  
  60. copyright();
  61.  
  62. }
  63.  
  64. sub head {
  65.  
  66. clean();
  67.  
  68. print qq(
  69.  
  70.  
  71.  @@@@@     @    @@@@    @   @@@@     @     @
  72.  @    @    @    @   @   @  @    @    @     @
  73.  @    @   @ @   @    @  @  @    @     @   @
  74.  @    @   @ @   @    @  @  @    @      @ @  
  75.  @@@@@   @   @  @    @  @  @    @       @  
  76.  @    @  @   @  @    @  @  @    @      @ @  
  77.  @    @  @@@@@  @    @  @  @    @     @   @
  78.  @    @ @     @ @   @   @  @    @    @     @
  79.  @    @ @     @ @@@@    @   @@@@     @     @
  80.  
  81.  
  82.  
  83. );
  84.  
  85. }
  86.  
  87. sub copyright {
  88. print "\n\n-- == (C) Doddy Hackman 2012 == --\n\n";
  89. <stdin>;
  90. exit(1);
  91. }
  92.  
  93. sub clean {
  94. my $os = $^O;
  95. if ($os =~/Win32/ig) {
  96. system("cls");
  97. } else {
  98. system("clear");
  99. }
  100. }
  101.  
  102. #The End ?
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


El problema ahora es que después de usar Control+C y querer cargar otra emisora, el script se olvida de usar el chdir() del directorio de mplayer y manda este error:

Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
"mplayer" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4


¿ Qué puedo hacer ahora con este error ?
BigBear
Perlero frecuente
Perlero frecuente
 
Mensajes: 981
Registrado: 2009-03-01 18:39 @818

Re: Problema con hashes

Notapor explorer » 2012-10-13 19:05 @837

Yo pondría la ruta completa a mplayer.

Quiero decir: hacer una vez el getcwd() y guardar el resultado en una variable, y usarla como ruta al mplayer.

Y no estás usando los array como debieras...
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Problema con hashes

Notapor BigBear » 2012-10-13 22:09 @965

Listo. Subí la línea de chdir() arriba del $SIG y problema resuelto.
BigBear
Perlero frecuente
Perlero frecuente
 
Mensajes: 981
Registrado: 2009-03-01 18:39 @818

Re: Problema con hashes

Notapor explorer » 2012-10-14 07:28 @352

¿Y los array?

¿Los has reducido a uno solo?

Pista: array de hashes
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Problema con hashes

Notapor BigBear » 2012-10-14 09:33 @440

No, no recuerdo o no sé cómo se hace un array de hashes.
BigBear
Perlero frecuente
Perlero frecuente
 
Mensajes: 981
Registrado: 2009-03-01 18:39 @818

Re: Problema con hashes

Notapor explorer » 2012-10-14 16:52 @744

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. my @emisoras = (
  2.     {
  3.         nombre => "Absolute Classic Rock (Broadband)",
  4.         genero => "Rock'n'Roll",
  5.         link   => 'http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vcbb',
  6.     },
  7.     {
  8.         nombre => "Absolute Classic Rock (Modem)",
  9.         genero => "Rock'n'Roll",
  10.         link   => 'http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=vc',
  11.     },
  12. );
  13.  
  14. for my $emisora (@emisoras) {
  15.     say "[+] Name   : $emisora->{nombre}";
  16.     say "[+] Station: $emisora->{genero}";
  17. }
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España


Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 24 invitados

cron