# Problema nº 17

########################################################

 

use strict;

 

my $salir = 0;

my $opcion = 0;

 

# Lo primero que haremos siempre es abrir el fichero donde se encuentra la base de datos.

# Este fichero es el articulos.txt.

# leemos los datos y creamos el array de hashes.

 

 

open (DB, "articulos.txt") or die "NO hay fichero\n";

my @articulos = ();

my @campos = ();

while ( <DB> ) {

 

      # El array campos contiene los 5 campos de cada articulo

      @campos = split ("::",$_);

           

      # $rec es una referencia al hash de cada articulo.

      my $ref = {};

      $ref->{"titulo"} = $campos[0];

      $ref->{"autores"} = $campos[1];

      $ref->{"revista"} = $campos[2];

      $ref->{"fecha"} = $campos[3];

      $ref->{"resumen"} = $campos[4];

     

      # Inserta un nuevo artículo en el array

      push @articulos, $ref;

      }

 

close (DB);

 

 

############

#

# Menú de opciones

#

############

 

my $mod = 0;

 

while ($salir == 0) {

      print "\n \t Elija una de las siguientes opciones"; 

      print "\n \t 1.- Introducir un nuevo elemento.";

      print "\n \t 2.- Listar todos los articulos.";

      print "\n \t 3.- Buscar articulo por titulo.";

      print "\n \t 4.- Buscar articulo por autor.";

      print "\n \t 5.- Listar todos los articulos de una determinada revista.";

      print "\n \t 6.- Salir.\n";

      $opcion = <STDIN>;

      chomp $opcion;

      if ($opcion == 1) {

            introducir(\@articulos);

            $mod = 1;

            }

      elsif ($opcion == 2) {

            listar_todo(\@articulos);

            }

      elsif ($opcion == 3) {

            buscar_tit(\@articulos);          

            }

      elsif ($opcion == 4) {

            buscar_autor(\@articulos);

            }

      elsif ($opcion == 5) {

            listar_revista(\@articulos);

            }

      elsif ($opcion == 6) {

            $salir = 1;

            if ($mod == 1) {

                  grabar_fichero(\@articulos);

                  }

            }

      else{}

}

 

exit;

 

 

########################################################

##

##    Zona de funciones

##

########################################################

 

sub introducir {

my $punt = $_[0];

 

my $ref = {};

my $temp;

 

print "\n Introduzca el titulo del artículo\n";

$temp = <STDIN>;

chomp $temp;     

$ref->{"titulo"} = $temp;

print "\n Introduzca los autores del artículo\n";

$temp = <STDIN>;

chomp $temp;     

$ref->{"autores"} = $temp;

print "\n Introduzca la revista en que fue publicada el articulo\n";

$temp = <STDIN>;

chomp $temp;     

$ref->{"revista"} = $temp;

print "\n Introduzca la fecha del artículo\n";

$temp = <STDIN>;

chomp $temp;     

$ref->{"fecha"} = $temp;

print "\n Introduzca el nombre del fichero donde se encuentra el resumen del articulo\n";

$temp = <STDIN>;

chomp $temp;     

$ref->{"resumen"} = $temp;

     

     

# Inserta un nuevo artículo en el array

push @articulos, $ref;

}

 

sub listar_todo {

 

my $punt = $_[0];

my $ref;

my $temp;

 

foreach $ref (@$punt) {

      $temp = $ref->{"titulo"};

      print "Titulo:\t$temp\n";

      $temp = $ref->{"autores"};

      print "Autores:\t$temp\n";

      $temp = $ref->{"revista"};

      print "Revista:\t$temp\n";

      $temp = $ref->{"fecha"};

      print "Fecha:\t$temp\n";

      print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";

      }

}

 

sub buscar_tit {

 

my $punt = $_[0];

my $ref;

my $temp;

my $pal;

 

print "\n Escriba la palabra que desea buscar en el titulo del articulo\n";

$pal = <STDIN>;

chomp $pal;

 

 

foreach $ref (@$punt) {

      $temp = $ref->{"titulo"};

 

      if ($temp =~ /$pal/) {

            print "Titulo:\t$temp\n";

            $temp = $ref->{"autores"};

            print "Autores:\t$temp\n";

            $temp = $ref->{"revista"};

            print "Revista:\t$temp\n";

            $temp = $ref->{"fecha"};

            print "Fecha:\t$temp\n";

            print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";

            }

      }

}

 

 

sub buscar_autor {

 

my $punt = $_[0];

my $ref;

my $temp;

my $pal;

 

print "\n Escriba el autor que desea buscar\n";

$pal = <STDIN>;

chomp $pal;

 

 

foreach $ref (@$punt) {

      $temp = $ref->{"autores"};

      if ($temp =~ /$pal/) {

            $temp = $ref->{"titulo"};

            print "Titulo:\t$temp\n";

            $temp = $ref->{"autores"};

            print "Autores:\t$temp\n";

            $temp = $ref->{"revista"};

            print "Revista:\t$temp\n";

            $temp = $ref->{"fecha"};

            print "Fecha:\t$temp\n";

            print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";

            }

      }

}

 

sub listar_revista {

 

my $punt = $_[0];

my $ref;

my $temp;

my $pal;

 

print "\n Escriba la revista de la que quiere listar los artículos\n";

$pal = <STDIN>;

chomp $pal;

 

 

foreach $ref (@$punt) {

      $temp = $ref->{"revista"};

      if ($temp =~ /$pal/) {

            $temp = $ref->{"titulo"};

            print "Titulo:\t$temp\n";

            $temp = $ref->{"autores"};

            print "Autores:\t$temp\n";

            $temp = $ref->{"fecha"};

            print "Fecha:\t$temp\n";

            print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";

            }

      }

}

 

 

sub grabar_fichero {

 

my $punt = $_[0];

my $ref;

my $temp;

open (DB, ">articulos.txt");

 

 

foreach $ref (@$punt) {

      $temp = $ref->{"titulo"};

      print DB "$temp\:\:";

      $temp = $ref->{"autores"};

      print DB "$temp\:\:";

      $temp = $ref->{"revista"};

      print DB "$temp\:\:";

      $temp = $ref->{"fecha"};

      print DB "$temp\:\:";

      $temp = $ref->{"resumen"};

      print DB "$temp\n";

            }

close (DB);

}