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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [Tszs] Need help with script: Edit Log

Chris Charley

Apr 22, 2018, 11:53 AM

Views: 5617
Re: [Tszs] Need help with script

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.

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){
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)

Edit Log:
Post edited by Chris Charley (User) on Apr 22, 2018, 4:57 PM

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

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