×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Numerical vs String variable

Numerical vs String variable

Numerical vs String variable

(OP)
I have an initial file I read in.  It is just a bunch of zeroes.  Max will be 400, looking like this.
0
0
0
0
0

If a certain event occurs, I want to increment that postition by 1.  Instead, my output looks like this
0
0
10
0
0

It appears to me it treats it aas a string and not as a number.  How do I fix this?

# Here is my code to open the file
open(ASKED, "$asked_data");
@asked = <ASKED>;
close(ASKED);


# Needless to say there is a lot of other code inbetween, but here is where I ask my question.  If it is true, I want the corresponding postion in the array given by $sequence to increase the number in $asked by one, i.e if is 0 become 1, if it is 1 become 2, etc..

if ($form{"$sequence[$i]"} eq $answers[$sequence[$i]] || $tick == 1) {
        $correct++;
        $num=$asked[$sequence[$i]];
        $num++;
        $asked[$sequence[$i]]=$num;    
        }
    }

# The last thing I do is write it to a file

open(ASKED, ">$asked_data");
print ASKED @asked;
close(ASKED);

Thanks
Wim Libby
libbys@camalott.com

RE: Numerical vs String variable

Well, despite not wanting to provoke further personal cries for help... I do already get enough email, thank you...  I'll answer anyway.  Guess I'm too nice for my own good ;)

Try this:

$num=int($asked[$sequence[$i]]);

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Numerical vs String variable

(OP)
Tom,

I do appreciate your help, but that did not fix it.  I still get
0
10
0
instead of
0
1
0
in my output file.  Any other suggestions?  If not, I do appreciate your time.

Wim

RE: Numerical vs String variable

Come to think of it, the original method you used should work just fine.  Are you sure that the 0 after the 1 isn't the one that belongs below that line?

Try this:

print ASKED "^@asked^";

If you get carrots (^) between the 1 and 0 then it is the method of storage that is the problem.  Try stripping all newlines and converting into a string first.

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Numerical vs String variable

(OP)
Tom,

Thanks for the suggestions.  Your last one made me try something and it worked!!!! Here is what I did:

if ($form{"$sequence[$i]"} eq $answers[$sequence[$i]] || $tick == 1) {
        $correct++;    
        $num = $asked[$sequence[$i]];
        $num++;
        $asked[$sequence[$i]] = "$num\n";

Thanks again!!
Wim libby

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close