Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: substr is behaving differently with small vs large strings

by Belgarion (Chaplain)
on Jul 07, 2004 at 21:03 UTC ( [id://372546]=note: print w/replies, xml ) Need Help??


in reply to substr is behaving differently with small vs large strings

Since you're opening a file anyway, you might want to consider using the seek and read methods on your filehandle. This will allow you to position the read pointer anywhere you like within the file and read in exactly the number of bytes you require. Also, you will not need to read the entire file into memory before doing the string manipulation.

Update: The above suggestion would still work, but you would need to take into account the formatted nature of your input file. To use the seek method correctly, you would need to take into account the spaces between segments and the leading numbers prefixing each string (and likely the newline which terminates each string.) Using seek would still work, but depending on the format of the full input file, it might not be feasible.

Update II: (Thinking some more...) I have a question regarding the input file and the offset. Is the offset from the textual beginning of the file, or from the beginning of the DNA sequence? It would appear that it's from the beginning of the DNA sequence, but I'm not going to assume here.

  • Comment on Re: substr is behaving differently with small vs large strings

Replies are listed 'Best First'.
Re^2: substr is behaving differently with small vs large strings
by perldeveloper (Scribe) on Jul 07, 2004 at 21:32 UTC
    The offset appears to make sense in the trimmed file. That is, excluding the spaces between the columns and the line numbers. Which suggests that Augustine might aviod slurping the file, by reading one line at a time (by leaving $/ unchanged) and windowing every input sequence (exon) (a sequence of lines which covers the exon).

    You could also use `seek' and `sysread' (as suggested by Belgarion, but you'd have to work out an algorithm (quite simple I would guess) which converts the address within the trimmed file to the address within the formatted file. Something like... $address_in_file = $address_in_trimmed_file + $number_of_characters_for_line_numbering_for_first_line + ($address_in_trimmed_file / 10) + ($address_in_trimmed_file / 60) * ($one_character_for_new_line + $number_of_characters_for_line_numbering). Actually, I think that is a correct and yet unabusive way to solve this (every 10 characters you get a space, every 60 characters you get a new line and a line number).

    On another note, though, I have experience with slurping large files, and can say that perl handles it pretty good. For example, I could semi-parse an 8Mb Xml file into an internal tree within seconds. I'm afraid I cannot say how substring handles large offset/length parameters, I thought it would work as expected.
Re^2: substr is behaving differently with small vs large strings
by wadunn (Initiate) on Jul 08, 2004 at 14:24 UTC
    Belgarion,

    Thanks for your input. As I said I am pretty a green Perl man. I am not familiar with how to decide how many bytes converts into a given number of charaters so I would just assume stay away from that.

    As for the input file, the textual begining of the file and the start of the DNA should be the same due to the editcontig sub or which ever of the new methods you guys suggested eating the numbers and the white space.

    Thanks again, for your time I can tell this is where i need to ask questions from now on. The other boards i frequent are not 1/8 as helpful as you guys.

    -Augustine

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://372546]
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-25 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found