Hi John.

See my reply in line.

John Doe wrote:
>
> Hello all,
> i have problem, and i don't know where is my mistake.
>
> ---
> #!/usr/bin/perl

  use strict;   # always
  use warnings; # usually

which means that all your variables need to be declared with 'my'.

> @myarra = ("test","error","block");

You may prefer

  my @myarra = qw( test error block );

which is a lot cleaner

> $file = 'sorted'; # File contain records xxx.xxx.xxx
> # i need last symbols after last -> .
> open(FILE, $file);

Always check the success of an open:

  open(FILE, $file) or die $!;

> while ($data = <FILE>) {
>     $backupdata = $data;

This is better written

  while (<FILE>) {
    $backupdata = $_;

>     $data =~ s/^(\S+)\.//g; # Remove xxx.xxx. get last 2 or 3 symbols
> after last -> .
>     $data = lc($data); # make $data to lowercast

Here is your problem. $data will still have the trailing newline
from the input file. A simple chomp() would help, but I think
your regex may also be wrong. Your comment says "i need last symbols
after last -> ." but your regex looks only up to the first whitespace
in the record. A $data value of "aaa.bbb ccc.ddd\n" will be changed
to "bbb ccc.ddd\n", and anything with leading spaces won't be changed
at all. Also you don't need the brackets which are capturing part of
the string you're deleting.

Solve both of these at once with:

  my ($data) = /.*\.(\S+)/

which will capture the last sequence of non-space characters (so
eliminating the newline) after the last dot.

>     for ($i = 0; $i < 3; ++$i)
>     {

  for my $i (0 .. $#myarra) {

>       if ($data eq $myarra[$i]) # no any outputs :(
> # if ($data =~ /$myarra[$i]/) # this not work
>     # $data contain -> est
>     # $myarra[$i] -> test
>     # and script say equal but not.
>         {
> print "------\n";
> print "$data\n";
> print "$i\n";
> print "$myarra[ $i ]\n";
> print "$backupdata\n"; # print $backupdata if
> # $data equal to @myarra[$i], in this case test = test, error = error
> # etc.
> print "------\n";
>         }
>     }
> }
>
> close (FILE);

The rest of your program should work. But I should also add that some
indenting and whitespace would help the readability of your work by an
enormous amount. I've added a composite of all the changes below.

HTH,

Rob


#!/usr/bin/perl

use strict;
use warnings;

my @myarra = qw/ test error block /;

my $file = 'sorted';

open(FILE, $file) or die $!;

while (<FILE>) {

  my $backupdata = $_;
  my ($data) = /.*\.(\S+)/;
  $data = lc($data);

  for my $i (0 .. $#myarra) {

    if ($data eq $myarra[$i]) {

      print "------\n";
      print "$data\n";
      print "$i\n";
      print "$myarra[ $i ]\n";
      print "$backupdata\n";
      print "------\n";
    }
  }
}

close (FILE);



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to