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 array behavior?

 



silverace99
Novice

Mar 22, 2010, 5:31 PM

Post #1 of 7 (1253 views)
Strange array behavior? Can't Post

Hi all, I'm writing a little macro that reads numbers from a text file and prints them out:

$count = 0;
my(@config);

for $line (<RANDGEN>) {
chomp($line); #trim terminating whitespace characters
$config[count] = atoi($line);
print "config[$count] = $config[count]\n";
$count++;
}

print "config[0] = $config[0]\n";

The results are printed below:

config[0] = 4 <- correct value
config[1] = 1
config[2] = 1
config[3] = 3
config[4] = 4
config[5] = 5
config[6] = 0
config[7] = 7
config[8] = 20
config[0] = 20 <---BUG, should still be 4! Why is it equal to config[8] O_o

For some reason, printing config[0] gets me the result of the last element in the array...what am I doing wrong?


7stud
Enthusiast

Mar 22, 2010, 5:55 PM

Post #2 of 7 (1251 views)
Re: [silverace99] Strange array behavior? [In reply to] Can't Post

1) Search google for 'code tags'. Read the first ten hits, then come back and post what you've learned.

2) All of your programs should start with these two lines:

Code
use strict; 
use warnings;

3)

Code
my $num = '10'; 
say atoi($num);

--output:--
Undefined subroutine &main::atoi called at 1perl.pl line 6.


4) Declare all variables with my();

5) Use variables instead of bareword filehandles:

Code
open my $INFILE, '<', 'data.txt';


6) count v. $count

What's the difference?


(This post was edited by 7stud on Mar 22, 2010, 5:57 PM)


silverace99
Novice

Mar 22, 2010, 6:06 PM

Post #3 of 7 (1246 views)
Re: [7stud] Strange array behavior? [In reply to] Can't Post

1) Ah sorry, I will keep that in mind in the future.

3) I grabbed a atoi function off a forum, didn't post it because I didn't think it was relevant to the problem

5) I don't see it, what does it do differently from what I'm doing now?

6) eeehhh thanks for the catch.

Thanks for your advice.

Got another problem though. Afterwards, I tried to put all this stuff into subexpressions, and now the code doesn't even go into the file-reading loop...my in-loop counter stays at 0.


Code
 
my @config = read_random("randomizer.txt");

foreach (@config) {
print $_;
print "\n";
}
#END

#open randomization file for reading
sub read_random {
my ($file) = @_;

open (my ($RANDGEN), "<$file") or die "couldn't open randomizer file: $!\n";

#save all lines of randomizer to an array
my ($count) = 0;
my (@config);
my ($line);
if (eof $RANDGEN) {
print "File was empty...\n";
}
else {
print "File has stuff in it\n";
}

for $line (<RANDGEN>) {
print "looping...\n";
print $line;
chomp($line); #trim terminating newline characters
push(@config, atoi($line));
$count++;
}
#*****************************************
print "count = $count\n";
close $RANDGEN;

return @config;
}

#convert text from randomizer file to integer
sub atoi {
my $t;
foreach my $d (split(//, shift())) {
$t = $t * 10 + $d;
}
return $t;
}


my randomizer file just contains some newline separated numbers:


Code
0 
1
2
3
4
5
6
7

Any ideas?


(This post was edited by silverace99 on Mar 23, 2010, 10:46 AM)


Nila
User


Mar 25, 2010, 10:21 PM

Post #4 of 7 (1217 views)
Re: [silverace99] Strange array behavior? [In reply to] Can't Post

You are missing the $ in file handler variable.


Code
for $line (<$RANDGEN>)



silverace99
Novice

Mar 26, 2010, 8:38 AM

Post #5 of 7 (1207 views)
Re: [Nila] Strange array behavior? [In reply to] Can't Post

yeah, finally caught that after an hour of scanning -_- thanks. I would put in the use strict global, but this code is going to be grafted onto someone else's code so I'm not sure if it will break things.


7stud
Enthusiast

Mar 27, 2010, 1:21 AM

Post #6 of 7 (1200 views)
Re: [silverace99] Strange array behavior? [In reply to] Can't Post

Of course, that doesn't make any sense. Writing code in a restrictive environment and then inserting into a less restrictive environment cannot break anything that you wouldn't have broken in the first place--while writing haphazard code under perl's horribly implemented original environment will have a tendency to break everything.


(This post was edited by 7stud on Mar 27, 2010, 1:23 AM)


silverace99
Novice

Mar 27, 2010, 3:09 PM

Post #7 of 7 (1191 views)
Re: [7stud] Strange array behavior? [In reply to] Can't Post

What I mean is that the code i am adding my stuff to is using a less restrictive environment...I didn't see any good methodology like explicit variable declaration. So I'm afraid if I slip in a "use strict", the code I didn't write could have problems?

 
 


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

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