Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Return full line from line number X

by CountZero (Bishop)
on Oct 26, 2008 at 23:03 UTC ( [id://719677]=note: print w/replies, xml ) Need Help??


in reply to Return full line from line number X

Almost there!

Within the loop that goes through all your lines:

my ($mnum,$mname) = split(/ /,$mdata,2); next unless $mnum == $linenumber_to_search_for; print "$mname"; last;

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Return full line from line number X
by Rathlar (Initiate) on Oct 26, 2008 at 23:23 UTC
    I guess I should have been more clear on that... the number field doesnt correlate with the line numbers

    there are 551 lines.. this is a static amount.

    think of it as a file with 2 fields one listing a phone number and the other listing a name.

    the line number will be used to pull the line and split it into the 2 variables which will then be used in another sub routine.

    basically this is an irc bot code for a game.

    users will /msg botname battle some_number

    which will then search the file for that line, grab the name of the monster, and the number which is it's attack rating and force the player and monster to fight based on both of their attack ratings.

      Perhaps you could show us a few lines from the file to give us a sample? So what you are saying is that the number you are looking for is more like an id number or phone number than a line number? So that when you say "the line number will be used to pull the line" you perhaps mean something more like the "phone number", to use your example?

      If there are no repeats of the line number you want from anywhere else in the file, then perhaps the following:
      undef $/; my $number = 42; #or $number = $ARGV[0] or whatever line number you were looking for my $file = <FILE>; my ($line, $name); # ($line) = $file =~ /$($number\s+.*)/m; # would store the whole line ($number, $name) = $file =~ /$($number)\s+(.*)/m; # use the second form since you just want number and name # and yes, you are overwriting the value of $number # with itself all over again. Use (undef, $name) = $file... # if that makes you feel more comfortable.
        no... there will be a list on a php page which will be numbered according to thier line position in the file... so i'll paste a couple lines from the file so you can get a better idea...

        258 Phontom Fungus
        266 Yeth Hound
        267 Mennlock
        269 Medium Earth Elemental

        the numbers represent the monster's attack value ($mnum) and the name is obviously $mname

        now lets say these are lines 51-54... lets say a player put in the command to battle $mline which in this case would be 53

        so $mline = 53 now which is the line i need to snag and then split to get the proper values.

        most of the other code in here doesn't really have anything to do with what I'm doing... its about 4k lines of code... but i could give you an example of the player sending the command via IRC... this would be the command that i'm making which would trigger the sub that I originally asked for help with.

        elsif ($arg[3] eq "battle") { if (!defined($username)) { privmsg("FIGHT Request Denied: You are not logged +in.", $usernick); } elsif ($rps{$username}{level} < 15) { privmsg("BATTLE Request Denied: Command available +to level 15+ users only.", $usernick, 1); } elsif ($arg[4] > 551) { privmsg("FIGHT Request Denied: This monster does n +ot exist.", $usernick, 1); } elsif ($arg[4] < 1) { privmsg("FIGHT Request Denied: This monster does n +ot exist.", $usernick, 1); } elsif (itemsum($username) > $arg[4]($mnum)) { privmsg("FIGHT Request Denied: Pick a harder monst +er!", $usernick, 1); } else { monster_battle($username, $arg[4]); } }
        basically most of it is just a bunch of checks to make sure the fight is possible... once we determine that, to starts the monster_battle sub pitting the player against the monster that was specified. $arg4 is the input the player gives which in this case should be the line number 53 meaning that the player wishes to fight with a "Mennlock" with an attack rating of "267"

        the line numbers will only be supplied on the php page which is just a quick reference to the player so that they can just type /msg botname battle 53

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://719677]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found