Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Return full line from line number X

by Rathlar (Initiate)
on Oct 26, 2008 at 22:23 UTC ( #719673=perlquestion: print w/ replies, xml ) Need Help??
Rathlar has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on some code here and I have no idea how to pull a full line by searching for the line number.

i have file mdata.txt and inside the lines are like this:

number name

Here is what I need to do: Pull the number and name from the txt file located at a user defined line number. Then split the 2 and assign them to variables.
splitting i can do... at least i think i have that part right...

my ($mnum,$mname) = split(/ /,$mdata,2);
I think that will split the line and assign the variables in one go unless I messed up there as well...

Basically I just need to search a file for a specific line number and then assign the text on that line to $mdata

Would anyone be able to help me figure this out? I'd be forever grateful for help with this.

Comment on Return full line from line number X
Download Code
Re: Return full line from line number X
by JavaFan (Canon) on Oct 26, 2008 at 22:39 UTC
    After opening the file into $file_handle
    while (<$file_handle>) { next if $. < $line_number_I_want; ... do whatever you want with this line ... last; }
      But that will only work if the line numbers in the file are starting at 1, increase by 1 for each line and have no gaps.

      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

Re: Return full line from line number X
by CountZero (Bishop) on Oct 26, 2008 at 23:03 UTC
    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

      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.
Re: Return full line from line number X
by lightoverhead (Monk) on Oct 27, 2008 at 02:08 UTC
    Try Tie::File , it can do as you wanted.
Re: Return full line from line number X
by oko1 (Deacon) on Oct 27, 2008 at 03:19 UTC

    Since your file is relatively small, it's a fairly simple task:

    #!/usr/bin/perl -w use strict; open F, '<', "myfile" or die "myfile: $!\n"; chomp(my @content = <F>); close F; # Input validation routine my $in; { print "Please enter the line number you want to retrieve (1-" . @c +ontent . "): "; chomp($in = <STDIN>); # line_numbers == array_indexes shifted by + 1 redo unless $in =~ /^\d+$/ && $in > 0 && $in <= @content; } # Now you can play with your retrieved value my ($mnum, $mname) = split / /, $content[$in - 1], 2; print "Number: $mnum Name: $mname\n";

    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://719673]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-09-22 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls