Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sprintf function 2

Status
Not open for further replies.

jisoo22

Programmer
Joined
Apr 30, 2001
Messages
277
Location
US
Hello!

I'm having a little trouble with the sprintf() function. What I'm trying to do is create a random number with the rand() function and then turn it into a non-fractional integer (ie 2 instead of 2.5 or whatever). The problem is that I don't quite understand the syntax for the sprintf function. Should my statement be like this:

$random = sprintf(%2c, $random);

Please let me know asap.

Thanks!
Jisoo22

 
Why don't you use the "int" function?

$Random=int(rand $Max);

 
Or you could use:

$random=(sprintf "%d", $random);
 
Unless you need a formatted number for printing, the int function raider2001 mentioned is probably the best way to go, especially if you want a whole number between 1 and n, and use the int(rand n) form. Note: if you DON'T use a maximum value and you use int, all you will ever get is 1! Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks for the suggestions! I couldn't use "int" before because I read somewhere that it doesn't do rounding. The basic premise of my little perl script is to open up a text document, count the number of lines of text within it, then randomly pick a line and display it. Unfortunately for me the "rand" function that gives me the random line number only gives back floats, so I'd most likely get a partial number like 2.57. So I'm guessing the "Sprintf" statement is the best way to go, correct?
 
P.S. Does anyone know the correct statement for printing out the contents in the line number? Is it printf?

Thanks,
Jisoo22
 
If you choose a random number between 0 and the number of lines in the file, you don't NEED to round it, Just use int to truncate it, and add 1.

printf will format and print a number. sprintf will format a number and store it in a string, from which you can print it later. Either one will work. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Ah that clears things up, thanks =) One thing about the printing though, I think I gave the wrong idea! My fault! I want to print what's actually on that line number instead of the actual line number itself. (i.e. it would print out "This is in line 1" if I selected 1). Would printf of sprintf still work?
 
If you aren't doing any formatting, "print" is the function you want. For formatting, use "printf".
 
Unless you need to format a number, just use print. As you read the contents of the text file, store them in an array (say @lines). Then you can print a random line with:
Code:
print $lines[rand $#lines],"\n";
In this case you really don't even need the int function, since perl will ignore the fractional part of the subscript anyway. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top