0

I am asked to do the perl program to find a value(from user input) in array. If matched "its ok". If not matched, then check within the value in the index[0] to index[1] ... index[n]. So then if the value matched to the between two elements then report which is near to these elements might be index[0] or index[1].

Let you explain.

Given array : 10 15 20 25 30; Get the value from user : 14 (eg.)

Hence 14 matched with in the two elements that is 10(array[0]) - 15(array[1])

Ultimately the check point is do not use more than one for loop and never use the while loop. You need to check one for loop and many of if conditions.

I got the output by which I did here is:

use strict;
use warnings;

my @arr1 = qw(10 15 20 25 30);
my $in = <STDIN>;
chomp($in);

if(grep /$in/, @arr1)
{  } #print "S: $in\n";  }
else
{
    for(my $i=0; $i<scalar(@arr1); $i++)
    {
        my $j = $i + 1;
        if($in > $arr1[$i] && $in < $arr1[$j])
        {
            #print "SN: $arr1[$i]\t$arr1[$j]\n";
            my ($inc, $dec) = "0";

            my $chk1 = $arr1[$i] + 1;
            AGAIN1:
            if($in == $chk1)
            { }
            else
            {  $chk1++; $inc++; goto AGAIN1;  }

            my $chk2 = $arr1[$j] - 1;
            AGAIN2:
            if($in == $chk2){ }
            else
            {  $chk2--;  $dec++; goto AGAIN2;  }
            if($inc > $dec)
            {  print "Matched value nearest to $arr1[$j]\n";  }
            elsif($inc < $dec)
            {  print "Matched value nearest to $arr1[$i]\n";  }
        }
    }
}

However my question is there a way in algorithm?. Hence if someone can help on this one and it would be appreciated.

Thanks in advance.

5
  • So you're looking to work as a perl programmer... Commented Jan 27, 2017 at 11:09
  • Fortunately I am a perl programmer. But Unfortunately I don't know how to do this in algorithm. Commented Jan 27, 2017 at 11:11
  • 1
    I don't think you are a Perl programmer. Why are you using lt and gt instead of < and >? Commented Jan 27, 2017 at 13:30
  • @DaveCross: Believe me. Not lie. Sometimes I made a mistake chilly. Commented Jan 27, 2017 at 14:03
  • 1
    @ssr1012: I agree with Dave Cross. A Perl programmer would not use a C-style for loop or an if condition with an empty block. I also doubt if the question intends you to avoid while loops by constructing them out of labels and goto. Commented Jan 27, 2017 at 14:45

2 Answers 2

3

You seem determined to make this as complicated as possible :-)

Your specification isn't completely clear, but I think this does what you want:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

my @array = qw[10 15 20 25 30];

chomp(my $in = <STDIN>);

if ($in < $array[0]) {
  say "$in is less than first element in the array";
  exit;
}

if ($in > $array[-1]) {
  say "$in is greater than last element in the array";
  exit;
}

for (0 .. $#array) {
  if ($in == $array[$_]) {
    say "$in is in the array";
    exit;
  }

  if ($in < $array[$_]) {
    if ($in - $array[$_ - 1] < $array[$_] - $in) {
      say "$in is closest to $array[$_ - 1]";
    } else {
      say "$in is closest to $array[$_]";
    }
    exit;
  }
}

say "Shouldn't get here!";
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. I know I created with more complexity. But you awesome.
2

Using the helper functions any and reduce from the core module List::Util and the built in abs.

#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw/reduce any/;

my @arr1 = qw(10 15 20 25 30);

chomp(my $in = <STDIN>);

if (any {$in == $_} @arr1) {
    print "$in is in the array\n";  
}
else {
    my $i = reduce { abs($in - $arr1[$a]) > abs($in - $arr1[$b]) ? $b : $a} 0 .. $#arr1;
    print "$in is closest to $arr1[$i]\n";  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.