CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need help with script

 



Tszs
New User

Apr 22, 2018, 8:28 AM

Post #1 of 5 (3171 views)
Need help with script Can't Post

Hey all, started learning perl so I need some help with one script:


Code
#!/usr/bin/perl 
print "How much elements in array n:\n";
$n=<>;
print "Number which you wanna change to 0:\n";
$a=<>;
for ($i=1 ; $i<=$n ; $i++){
print "Enter array element:\n";
$array[$i]=<>;
chomp($array[$i]);
}
foreach (@array){
s/$a/0/;
}
print "New array: @array \n";


I need to change all elements which equal to $a which I enter, to 0.


(This post was edited by Tszs on Apr 22, 2018, 8:29 AM)


Chris Charley
User

Apr 22, 2018, 11:53 AM

Post #2 of 5 (3158 views)
Re: [Tszs] Need help with script [In reply to] Can't Post

There were a few errors that using 'warnings' would have caught. You need to 'chomp $n and $a ($a and $b are reserved for the sort function and should be avoided in your script unless using in a sort).

Your for loop starts at 1 but should start at 0. (and then the loop exit condition would be '$i < $n;'

Using strict and warnings, as you should on all your programs, would give the following result.

My code above fixes those problems.


Code
#!/usr/bin/perl 
use strict;
use warnings;

my @array;

print "How much elements in array n:\n";
chomp(my $n=<>);
print "Number which you wanna change to 0:\n";
chomp(my $x=<>);
for (my $i=0 ; $i<$n ; $i++){
print "Enter array element:\n";
chomp($array[$i] = <>);
}

foreach (@array){
s/$x/0/;
}
print "New array: @array \n";


Update: I forgot to mention what errors you had in your script. The first one was not applying chomp to $a. Later, when doing a substitution, no items would be substituted because $a has a number AND a newline (that should've been chomped). Since you chomped all the numbers in the array, they can't be matched to $a which has a trailing newline.

The second problem is that you are beginning your array index, $i, at 1 instead of 0. If you had warnings enabled, you would have received a warning about an uniniallized value when you executed the line print "New array: @array \n"; That would be because $array[0] is not initialized.


(This post was edited by Chris Charley on Apr 22, 2018, 4:57 PM)


Tszs
New User

Apr 22, 2018, 1:27 PM

Post #3 of 5 (3147 views)
Re: [Chris Charley] Need help with script [In reply to] Can't Post

Thanks for suggestion to use strict and warning, they really helped for other programs :) btw wanna ask one more question since already started thread.Need to write a script which counts and prints how much same elements are in array ( I insert that element with keyboard ) and if there are more than one same element, I need to print them place in array ( index ).
So I know how to create an array, but how to check how much times that element which I enter repeats in array and also how to print all those elements places in array ?


BillKSmith
Veteran

Apr 22, 2018, 9:07 PM

Post #4 of 5 (3135 views)
Re: [Tszs] Need help with script [In reply to] Can't Post

Modules can be a big help even in a small script.

Code
C:\Users\Bill\forums\guru>type tszs.pl 
use strict;
use warnings;
use IO::Prompt::Hooked;
use Readonly;
use List::MoreUtils qw(indexes);

Readonly::Array my @array => qw( a s d f g h j k l e v x s f i l e g d a l );

my $element = prompt("Please enter the search character");

if(my @found = indexes {$_ eq $element} @array) {
print "Element '$element' appears at the following location(s): \n\t";
print "@found\n";
}
else {
print "Element '$element' does not appear in array\n";
}




C:\Users\Bill\forums\guru>perl tszs.pl
Please enter the search character l
Element 'l' appears at the following location(s):
8 15 20

C:\Users\Bill\forums\guru>perl tszs.pl
Please enter the search character m
Element 'm' does not appear in array

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Apr 22, 2018, 11:45 PM

Post #5 of 5 (3132 views)
Re: [Tszs] Need help with script [In reply to] Can't Post


In Reply To
but how to check how much times that element which I enter repeats in array and also how to print all those elements places in array ?


Bill has shown you the use of a module, but since you're a beginner, I think it would be useful for you to learn how to do it using pure Perl syntax.

The idea would be to loop over the array indices and keep those for which your condition is met. For example, to find the indices of @array where the alue is tge letter 'a', something like this (untested, I don't have Perl installed on my mobile device):


Code
my @matching_indices = grep { $array[$_] eq 'a' } 0..$#array;


or, if you prefer a for loop:

Code
my @matching_indices; 
for my $idx (0..$#array) {
push @matching_indices, $_ if $array[$idx] eq 'a';
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives