数组和子例程的数组

时间:2016-04-11 17:54:08

标签: arrays perl substr

我在使用数组和子程序数组的perl中遇到了困难。我正在尝试单词梯形图问题,我想在给出一些字典的两个单词之间找到一些步骤序列。当我尝试运行我的perl程序时,我会在字符串比较步骤中收到警告。这是我的简化方法:

#!/usr/bin/perl -w
use strict;
use warnings;

my @list = ("this", "that", "thin", "thus", "past");
my $myword = "thip";
my $end = "thus";
my @aoa = ([$myword]);
my $diff_chars = 0;
my $i = 0;

foreach (@aoa){
  #push a word from @list with one letter difference onto our $_
  push $_, routine($_, $_->[-1]);
  push @aoa, $_;
  #if we the last element in our list matches the end then we are done
  if($_->[-1] eq $end){
    print "we found a path via $_\n";
    last;
  }
}

sub routine{
  my @arr = @{$_[0]};
  my $mines = $_[1];
  my $index = 0;
  foreach my $word (@list){
    $diff_chars = 0;
    $i = 0;
    $index++;
    next if grep{$_ eq $word} @arr;
    foreach my $char (split //, $word){
      if($char ne substr $mines, $i, 1){
        $diff_chars++;
      }
      $i++;
    }
    if($diff_chars == 1){
      #remove $word from list to remove possible duplicates
      splice @list, $index, 1;
      return $word;
    }
  }
}

运行时,我得到一个无限循环,继续输出警告

substr outside of string at t.pl line 33.
Use of uninitialized value in string ne at t.pl line 33.

这是我的if($char ne substr $mines, $i, 1)行。我不确定为什么我得到这个,并且一直在尝试修复此错误几个小时,并且可以真正使用一些帮助。

0 个答案:

没有答案