Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^3: Return full line from line number X

by krusty (Hermit)
on Oct 27, 2008 at 00:47 UTC ( #719680=note: print w/ replies, xml ) Need Help??

in reply to Re^2: Return full line from line number X
in thread Return full line from line number X

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.
Comment on Re^3: Return full line from line number X
Download Code
Replies are listed 'Best First'.
Re^4: Return full line from line number X
by Rathlar (Initiate) on Oct 27, 2008 at 02:51 UTC
    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?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2016-05-25 11:57 GMT
Find Nodes?
    Voting Booth?