Skip to content

Instantly share code, notes, and snippets.

@am0c
Last active December 15, 2015 05:48
Show Gist options
  • Save am0c/dc65b37eca606e74e760 to your computer and use it in GitHub Desktop.
Save am0c/dc65b37eca606e74e760 to your computer and use it in GitHub Desktop.
배열안에서 서브배열의 인덱스를 찾는 모듈이 있나염
#/usr/bin/env perl
use 5.014;
use warnings;
use strict;
use Carp;
sub find_in_array {
my ($array, $key, $offset) = @_;
my $array_last = $#$array;
my $key_last = $#$key;
if (!defined($offset)) {
$offset = 0;
}
else {
carp "offset you request is higher than array size" if $offset > $array_last;
}
for my $i ($offset .. $array_last) {
next if $i + $key_last > $array_last;
my $is_eql = 1;
for my $j (0 .. $key_last) {
if ($key->[$j] ne $array->[$i + $j]) {
$is_eql = 0;
last;
}
}
return $i if $is_eql;
}
return;
}
my @arr = qw(A B C D E F G);
my @key = qw(D E F);
say find_in_array \@arr, \@key;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment