From 398700e9ff87da8f264f4c4a26c5ac22f0fa6c77 Mon Sep 17 00:00:00 2001 From: Prateek Jena <34654465+prateikjena@users.noreply.github.com> Date: Sat, 5 Oct 2019 21:39:04 +0530 Subject: [PATCH] Add perl language. (#170) Related to #1 --- README.md | 1 + .../javascript-Linear-Search-2.js | 28 ++++++++++++++--- perl-linear-search/README.md | 12 ++++++++ perl-linear-search/perl-linear-search.pl | 30 +++++++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 perl-linear-search/README.md create mode 100644 perl-linear-search/perl-linear-search.pl diff --git a/README.md b/README.md index d37525c..3d371de 100755 --- a/README.md +++ b/README.md @@ -42,3 +42,4 @@ Linear search is a very simple search algorithm. In this type of search, a seque |29| [Aditya Jyoti Paul](https://github.com/phreakyphoenix) | SRM IST | India | Cpp | |30| [Tejas Agarwal](https://github.com/tjzs69) | Manipal University Jaipur | India | Java | |31| [Chandan Taneja](https://github.com/chandantaneja) | Manav Rachna University | India | Java +|32| [Prateek Jena](https://github.com/prateikjena) | Bhadrak Autonomous College | India | Perl | \ No newline at end of file diff --git a/javascript-linear-search/javascript-Linear-Search-2.js b/javascript-linear-search/javascript-Linear-Search-2.js index ceacabf..494e9d8 100644 --- a/javascript-linear-search/javascript-Linear-Search-2.js +++ b/javascript-linear-search/javascript-Linear-Search-2.js @@ -1,6 +1,26 @@ -function linearSearch(arr, key){ - for(let i = 0; i < arr.length; i++){ - if(arr[i] === key) return i; +function linear () { + let n = parseInt(prompt('Enter the size of an array')) + let a = [] + for (let i = 0; i < n; i++) { + a[i] = parseInt(prompt('Current array:\t' + a + '\nEnter array elements')) + } + let k = parseInt(prompt('Current array:\t' + a + '\nEnter the key element to search: ')) + for (let i = 0; i < a.length; i++) { + if (k === a[i]) { + // document.body.innerText(''); + document.writeln('Element ' + a[i] + ' Found at Position:' + i) + break + } else if (i === (a.length - 1) && k !== a[i]) { + // document.body.innerText(''); + document.writeln('Element Not Found') + } + } +} +const nums = [1, 2, 3, 8, 9, 12] +const target = 2 +for (const num of nums) { + if (num === target) { + console.log('Successful Search!') + break } - return null; } diff --git a/perl-linear-search/README.md b/perl-linear-search/README.md new file mode 100644 index 0000000..b36186c --- /dev/null +++ b/perl-linear-search/README.md @@ -0,0 +1,12 @@ +# Linear Search in perl + +Example linear search code in perl language. + +### Run + +```bash +$ ./perl perl-linear-search.pl +``` + + +Pay attention to the way certain shells split words and parse input! diff --git a/perl-linear-search/perl-linear-search.pl b/perl-linear-search/perl-linear-search.pl new file mode 100644 index 0000000..400eddd --- /dev/null +++ b/perl-linear-search/perl-linear-search.pl @@ -0,0 +1,30 @@ +#Take the input for the array +print "Enter the no of elements in the array "; +chomp ( $size = ); +print "Enter the values: "; +for ( $i =0; $i <$size; ++$i){ + my $num = ; + chomp $num; + push @array, $num; +} + +# prompt the user for a search key +print "Enter an integer search key: "; +chomp ( $searchKey = ); + +$found = 0; # $found is initially false + +for ( $i = 0; $i < @array && !$found; ++$i ) { + + if ( $array[ $i ] == $searchKey ) { + $index = $i; + $found = 1; + } +} + +if ( $found ) { # $found == 1 + print "Found $searchKey at subscript $index \n"; +} +else { # $found == 0 + print "$searchKey not found \n"; +}