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:
Strange error

 



H.Adam
New User

Aug 27, 2009, 10:21 AM

Post #1 of 3 (327 views)
Strange error Can't Post

I made a very short program, which opens a file and loads it to array. But in the 1. row the 1. character is missing. In the 2. row the 2. character is missing, in the 3. the 3. etc...

I don't know what's the error in the code.


Code
open(PROG,'<program.txt'); 
@rows=<PROG>;
close(PROG);
$prognum=@rows[0];
for($a=1;$a<=$prognum;$a++){
print "$a\n";
for($b=0;$b<=length(@rows[$a])-2;$b++){
@prog[$a,$b]=substr(@rows[$a],$b,1);
print "@prog[$a,$b]";
}
print "\n";
}


The the first row of the opened file must contain a number, which is the number of the following rows.


FishMonger
Veteran / Moderator

Aug 27, 2009, 10:43 AM

Post #2 of 3 (326 views)
Re: [H.Adam] Strange error [In reply to] Can't Post

Add these 2 lines to the beginning of the script and fix the problems that they point out.

Code
use strict; 
use warnings;


If that doesn't solve this issue, then post more details, such as the contents of program.txt, and the desired output from the script.


(This post was edited by FishMonger on Aug 27, 2009, 10:44 AM)


sir_com
New User

Aug 27, 2009, 11:56 AM

Post #3 of 3 (320 views)
Re: [FishMonger] Strange error [In reply to] Can't Post

Hi,

Though I am really confused as what you are trying to do, I have tried to modify a little of your code as below

#!/bin/perl

use strict;

open(PROG,'program.txt') or die ("can't open for $!\n");
my @rows=<PROG>;
close(PROG);

my $prognum=@rows;

for(my $a=1;$a<=$prognum;$a++)
{
print "$a\n";

for(my $b=0;$b<=(length($rows[$a])-2);$b++)
{
my @prog=substr($rows[$a],$b,1);
print "@prog";
}
print "\n";
}


Output would be something like:

1
which opens a file and loads it to array
2
I don't know what's the error in the code
3


Please be specific about what you are trying to do and all. Also, in your posted code you are using

@prog[$a,$b] --> a multidimensional array, like a matrix which is not supported. If you want it that way, you have to use referrence.

Thanks,

sir_com

 
 


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

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