Skip to content
This repository has been archived by the owner on Dec 29, 2019. It is now read-only.

Commit

Permalink
Add perl language. (#170)
Browse files Browse the repository at this point in the history
Related to #1
  • Loading branch information
prateikjena authored and ayan-b committed Oct 5, 2019
1 parent d3e590e commit 398700e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
28 changes: 24 additions & 4 deletions javascript-linear-search/javascript-Linear-Search-2.js
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions perl-linear-search/README.md
Original file line number Diff line number Diff line change
@@ -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!
30 changes: 30 additions & 0 deletions perl-linear-search/perl-linear-search.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#Take the input for the array
print "Enter the no of elements in the array ";
chomp ( $size = <STDIN> );
print "Enter the values: ";
for ( $i =0; $i <$size; ++$i){
my $num = <STDIN>;
chomp $num;
push @array, $num;
}

# prompt the user for a search key
print "Enter an integer search key: ";
chomp ( $searchKey = <STDIN> );

$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";
}

0 comments on commit 398700e

Please sign in to comment.