Array Indexing problem

H

Harish

Hi,

I am having a problem while indexing an element in perl.

Problem Details:

I have a data file which looks like the below

0.5 2 0.01 0 92 44 19818
0.5 2 0.015 0 62 42 19869
0.5 2 0.02 0 49 37 19902
0.5 2 0.025 0 40 30 19920
0.5 2 0.03 0 32 27 19935
0.5 2 0.035 0 28 31 19942
0.5 2 0.04 0 23 24 19952


I need to selectively take few lines from this file store it into
another file.
I'm doing this by storing the file into an array. Then i would
generate the index where the line that i'm interested in is located
and then access the line and store in another file.My program seems to
be working fine for everything except for few lines.

if i have $x=4 then when i say $temp[$x] it prints the previous line
but when i say $temp[4] it prints the line that i'm interested in.
This does not happen for all the lines.It is just happening for few
lines.

Could u let me know where I'm going wrong here.

Thanks
Harish

P.S.

I'm attaching the code below. Here $x is used to generate the index
number.There is nothing wrong in the logic of genrating $x.



if (!open(INFILE,"<f1.dat"))
{
die("cannot open file data.dat\n");
}
@temp = <INFILE>;
close(INFILE);
if (!open(OUTFILE,">testdata1.dat"))
{
die("cannot open file validation$a.dat\n");
}
@aspect = (0.5);
@delay=(0.01,0.02,0.03);
@msizes = (2);

foreach $line1 (@aspect)
{
$p =($line1-0.5)*20;
foreach $line2 (@msizes)
{
$q = ($line2-2);
foreach $line3 (@delay)
{
$r = (($line3-0.01)*200);
$x = $p*836+$q*19+$r;
print OUTFILE "$temp[$x]\n";
}
}
}
 
P

Pedro Graca

Harish wrote:
[snip]
$r = (($line3-0.01)*200);
$x = $p*836+$q*19+$r;
print OUTFILE "$temp[$x]\n";

are you sure $x is an integer? print it!

$ perl -e '$x=1.99998; @t=(1, 2, 3, 4); print $x, " ==> ", $t[$x], "\n";'

prints
1.99998 ==> 2 [as if $x was 1]
 
J

John W. Krahn

Harish said:
I am having a problem while indexing an element in perl.

Problem Details:

[snip]

if i have $x=4 then when i say $temp[$x] it prints the previous line
but when i say $temp[4] it prints the line that i'm interested in.
This does not happen for all the lines.It is just happening for few
lines.

You are running in to the classical problem of representing floating
point numbers on a binary computer. When you use a floating point
number as an array index, perl will convert it to an integer by removing
everything after the decimal point.

$ perl -e'
my @array = 0 .. 10;
my $x = 4 - 1e-15;
print "$x\n";
printf "%.18f\n", $x;
print "$array[$x]\n";
'
4
3.999999999999999112
3


This FAQ from perlfaq4.pod will help explain it:

perldoc -q "Why am I getting long decimals"



John
 
H

Harish

Hi ,
Thanks a lot for all the information.I finally figured out the problem
anfd my code is working fine.
Thanks
Harish

John W. Krahn said:
Harish said:
I am having a problem while indexing an element in perl.

Problem Details:

[snip]

if i have $x=4 then when i say $temp[$x] it prints the previous line
but when i say $temp[4] it prints the line that i'm interested in.
This does not happen for all the lines.It is just happening for few
lines.

You are running in to the classical problem of representing floating
point numbers on a binary computer. When you use a floating point
number as an array index, perl will convert it to an integer by removing
everything after the decimal point.

$ perl -e'
my @array = 0 .. 10;
my $x = 4 - 1e-15;
print "$x\n";
printf "%.18f\n", $x;
print "$array[$x]\n";
'
4
3.999999999999999112
3


This FAQ from perlfaq4.pod will help explain it:

perldoc -q "Why am I getting long decimals"



John
 
A

Anno Siegel

Harish said:
Hi,

I am having a problem while indexing an element in perl.

Problem Details:

I have a data file which looks like the below

0.5 2 0.01 0 92 44 19818
0.5 2 0.015 0 62 42 19869
0.5 2 0.02 0 49 37 19902
0.5 2 0.025 0 40 30 19920
0.5 2 0.03 0 32 27 19935
0.5 2 0.035 0 28 31 19942
0.5 2 0.04 0 23 24 19952


I need to selectively take few lines from this file store it into
another file.
I'm doing this by storing the file into an array. Then i would
generate the index where the line that i'm interested in is located
and then access the line and store in another file.My program seems to
be working fine for everything except for few lines.

if i have $x=4 then when i say $temp[$x] it prints the previous line
^^^^
Look closer. I bet you don't have $x == 4, it only prints as '4'.
but when i say $temp[4] it prints the line that i'm interested in.
This does not happen for all the lines.It is just happening for few
lines.

Could u let me know where I'm going wrong here.
^
Please use complete English words. This kind of jargon makes you look
like a wannabe.
Thanks
Harish

P.S.

I'm attaching the code below. Here $x is used to generate the index
number.There is nothing wrong in the logic of genrating $x.

Sez you!
if (!open(INFILE,"<f1.dat"))
{
die("cannot open file data.dat\n");
}
@temp = <INFILE>;
close(INFILE);
if (!open(OUTFILE,">testdata1.dat"))
{
die("cannot open file validation$a.dat\n");
}
@aspect = (0.5);
@delay=(0.01,0.02,0.03);
@msizes = (2);

foreach $line1 (@aspect)
{
$p =($line1-0.5)*20;
foreach $line2 (@msizes)
{
$q = ($line2-2);
foreach $line3 (@delay)
{
$r = (($line3-0.01)*200);
$x = $p*836+$q*19+$r;

Your $x is not an integer. I bet the undisclosed logic behind the
calculations is such that it *should* be an integer for the values
you calculate. But non-integer arithmetic is imprecise, and the
result may be just a tad smaller than it should be. When this number
is used as an array index, it is truncated to the next smaller integer,
so the resulting index is 3 instead of the expected 4. See
"perldoc -q decimals" for more on this.

The remedy is to round the result (instead of letting it be truncated):

$x = sprintf "%.0f", $p*836+$q*19+$r;

See "perldoc -q round" for this use of sprintf.
print OUTFILE "$temp[$x]\n";
}
}
}

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top