Help a a begineer, easy script.

    • Help a a begineer, easy script.

      Hi,

      I´m trying to learn Perl and I´ve read some tutorial sites but when I put what I read in my script I´m not having what I am trying to develop.

      I think what I want to develop is easy because I´ve done the same in a few lines in Shell. So thanks in advance if someone helps me and correct my mistakes with the following perl script.

      Firstly, I have a text file from a SQL Plus query, with lines separated by "|". I want to read line by line, assign the fields in the line to variables (an array for example). Then check if the field of this line is found in another lists (could be another array or hash), if it is not found, then push the field to the list and reorganize the line (4 fields in a required format) to print in a text file. (hope this is understandable).

      To sum up:

      #!/opt/pin/5.5/bin/perl

      use strict;
      use warnings;
      use List::Util qw(first);

      my $file = './test.out';
      my @pines;
      my $out_file = "lista_perl.txt";

      open(my $data, '<', $file) or die "Could not open '$file' $!\n";

      while (my $line = <$data>) {

      print "LINEA=[$line]\n";
      my @fields = split "|" , $line;
      print "F1=[$fields[0]]";
      print "F2=[$fields[1]]";
      print "F3=[$fields[2]]";
      print "F4=[$fields[3]]";

      #s{^\s+|\s+$}{}g foreach @fields; -- not working fine
      #@fields=trim(@fields);
      #for (@fields) {
      # s/\s+$//;
      #
      #}
      #for (@fields) { chop() while substr($_,-1,1) eq ' ' } not working either
      @fields = map{ (s/\s*$//)&&$_}@fields;

      print "FIELDS=[@fields]\n";

      if (my $first_pin = first { $_ eq $fields[3] } @pines)
      {
      next;
      }

      #conitnue if /@fields[3]/ ~~ %pines; not working in 5.8.3
      # add one element at the end of the array
      # want to add #6666# where 6666=field[3], want to add with "#" in order to avoid to get a true in the first for the cases 666 is found in 6666
      push(@pines, "'#'.$fields[3].'#'");

      print "PINES=[@pines]\n";
      my @list = ("$fields[0].'|'", "$fields[1].'|'", "$fields[2].'|'", "$fields[3]");
      print "LISTA=[@list]\n";

      # Can not use print "@list" >> $out_file, this does not work in perl
      open(my $output, '>>', $out_file) or die "Could not open '$file' $!\n";
      print $output "@list\n" ;
      close($output);

      last if $. == 5;
      #my $match_found = any { /pattern/ } @list_of_strings; try this in case not working above

      }

      close($data);
      exit;

      ==========================

      RESULT:

      LINEA=[ 8253| 339378191|Gestion Cisco 1841 |8386808
      ]
      F1=[ ]F2=[ ]F3=[ ]F4=[ ]FIELDS=[ 8 2 5 3 | 3 3 9 3 7 8 1 9 1 | G e s t i o n C i s c o 1 8 4 1 | 8 3 8 6 8 0 8 ]
      PINES=['#'..'#']
      LISTA=[.'|' .'|' .'|' ]
      LINEA=[ 8253| 339378207|Mantenimiento Avanzado Cisco 1841 |10889889
      ]
      F1=[ ]F2=[ ]F3=[ ]F4=[ ]FIELDS=[ 8 2 5 3 | 3 3 9 3 7 8 2 0 7 | M a n t e n i m i e n t o A v a n z a d o C i s c o 1 8 4 1 | 1 0 8 8 9 8 8 9 ]
      PINES=['#'..'#' '#'..'#']
      LISTA=[.'|' .'|' .'|' ]
      LINEA=[ 8253| 339378231|Abono Facilidad FAS |6518570
      ]
      F1=[ ]F2=[ ]F3=[ ]F4=[ ]FIELDS=[ 8 2 5 3 | 3 3 9 3 7 8 2 3 1 | A b o n o F a c i l i d a d F A S | 6 5 1 8 5 7 0 ]
      PINES=['#'..'#' '#'..'#' '#'..'#']
      LISTA=[.'|' .'|' .'|' ]
      LINEA=[ 8253| 339378295|Abono Facilidad FAS |6570649
      ]
      ...


      1) Don´t understand why the Split is not working as I expected.
      2) Don´t understand why the map is not working as I expected either.
      3) The filelds are empty??

      Well any explanation and advise and recommendations is well appreciated.

      And for last, sorry for my english :)

      Thanks!!