|
My huge thanks to all of you for the insights -- I've now stopped doubting my own sanity and I'm back to doubting everyone else's. I'll keep on coding, and I'll definitely look up Perl Best Practices and Modern Perl. And continue to curse the universe when I run across a piece of code that looks something like $'\\_&'":_#{'"@(`$)};
|
# ? Jun 23, 2013 19:01 |
|
|
# ? May 22, 2024 15:11 |
|
Without default variables, this idiom would be much noisier:Perl code:
|
# ? Jun 24, 2013 07:08 |
|
Peanutmonger posted:Without default variables, this idiom would be much noisier:
|
# ? Jun 24, 2013 07:34 |
|
It also changes the global value of $_, so if another part of the code tries to take similar shortcuts you'll run into problems. for/map/grep at least localize $_, so they don't suffer as much from this issue.
|
# ? Jun 24, 2013 12:20 |
|
The main thing to keep in mind when reading Perl Best Practices (and to be fair it's mentioned in the preface) is that the book's contents are not to be taken as the gospel truth. As welcome to hell said, about half of what it recommends you should avoid outright (inside-out objects, anyone?). Also, Higher-Order Perl is probably one of the better Perl books ever written, and is available for free from the author, so check that out too.
|
# ? Jun 25, 2013 19:08 |
|
welcome to hell posted:It also changes the global value of $_, so if another part of the code tries to take similar shortcuts you'll run into problems. for/map/grep at least localize $_, so they don't suffer as much from this issue. Edit: And now I see you mentioned for loops. And when I run nested while (<>) loops it does trample the $_ variable. code:
Preloop: #! /usr/bin/perl -w Inner: Postloop: Preloop: use strict; Inner: Postloop: Preloop: while (<>) { Inner: print "Preloop: $_"; Postloop: print "Preloop: $_"; Preloop: while (<>) { Inner: print "Inner: $_"; Postloop: print "Inner: $_"; Preloop: last; Inner: } Postloop: } Preloop: print "Postloop: $_"; Inner: } Postloop: } Preloop: Inner: exit; Postloop: exit; Preloop: Use of uninitialized value $_ in concatenation (.) or string at ./test.pl line 11, <> line 15. Postloop: ephphatha fucked around with this message at 02:51 on Jun 26, 2013 |
# ? Jun 26, 2013 02:45 |
|
Ephphatha posted:Edit: And now I see you mentioned for loops. And when I run nested while (<>) loops it does trample the $_ variable.
|
# ? Jun 26, 2013 03:16 |
|
code:
code:
|
# ? Sep 9, 2013 20:57 |
|
eXXon posted:
code:
code:
|
# ? Sep 9, 2013 21:09 |
|
Oh, so it suffices to write my ($first) = multi_array();?
|
# ? Sep 9, 2013 21:16 |
|
eXXon posted:Oh, so it suffices to write my ($first) = multi_array();? Yup. Try it out.
|
# ? Sep 9, 2013 21:24 |
|
eXXon posted:
Here is a less redundant version of your code. It doesn't answer your (already answered) question but when you avoid doing redundant stuff and silly things like assigning values to undef things become easier to understand. code:
uG fucked around with this message at 18:53 on Sep 15, 2013 |
# ? Sep 15, 2013 18:26 |
|
Anyone here have experience using the Perl Net::SNMP::Agent modules to write NetSNMP agent handlers? I am having issues returning a table via an snmp walk. Essentially I want to reply to an SNMP query with some data I have in an MySQL db (about 250 rows with 15 values in each row). So, the structure, from what I can gather from other tables of SNMP Data Ive compared to, should be like rootOID.ROW.VALUE, returned as all rows for value 0 first, then value 1 second, etc etc. What happens is when I get the query in, the script returns rootOID.0.0 through rootOID.250.0, then sets the next OID to rootOID.0.1, yet the SNMPwalk never seems to continue. Relevant code: code:
Another option I have (and may need to go with), is that I can compile C modules into NetSNMP as custom handlers, and there is a whole bunch of stuff in the NetSNMP .h's pertaining to registering and delivering tables. Why those aren't in the Perl API I don't know but it looks like if I can manage to get out of my own way hacking together some C code it would be theoretically easier.
|
# ? Sep 16, 2013 15:41 |
|
Where does this 'walk' take place in the code? You are also using NetSNMP::Agent, not Net::SNMP::Agent or Net::SNMP from what I can see. What values can we pass to your function and what are the expected results?
|
# ? Sep 16, 2013 17:53 |
|
The snmpwalk takes place externally, snmpwalk being a commandline utility to issue to a sever providing snmp responses. The snmpwalk sends a GETNEXT request to an snmp server, the server replies with the next valid OID in a sequence based on that one, along with the value of the response for the OID the next request was sent for. An example: I issue 'snmpwalk somesnmpserver 1.2.3' SNMP server says 'ok, 1.2.3's first valid oid is 1.2.3.0.0, I will send the requestor the value of 1.2.3.0.0, and then let them know the next OID is 1.2.3.1.0', at which point the snmpwalker will send another GETNEXT request for 1.2.3.1.0, get a value for it along with the next valid OID in the sequence, and so on. What is passed into the handler is from NetSNMP::Agent, which is $handler, $registration_info, $request_info, and $requests. What we're mainly dealing with is $request_info (an object containing things like what type of request this is, ie. a simple GET, a GETNEXT, GETBULK, etc), and $requests, an arrayref of request objects. The request objects contain the OID the requestor is asking for a value for, and then we also then modify these request objects with what we wish to return, the NetSNMP daemon running on the machine handles delivering that back to the requestor. We access that by just saying $request->setValue( TYPE, $value ), and we can set the nextOID in the case of a getnext request with $request->setOID( NetSNMP::OID object ); Anyhow, it's and odd and awkward thing to ask for help on here because aside from being sloppy, lazy and not as professional as others' code might be, it's doing what I am asking and intending it to do, and I think the problem lies in how I'm attempting to implement an SNMP table. I guess it was mainly on the hope that someone else here has used perl to do such an implementation via NetSNMP::Agent, because I have googled every combo of 'Perl Net::SNMP agent table' and related phrases I can think of and found NOTHING. Also, the S in SNMP is the biggest loving joke of a lie ever.
|
# ? Sep 16, 2013 18:10 |
|
Have you looked at Net::SNMP? http://search.cpan.org/~dtown/Net-SNMP-v6.0.1/lib/Net/SNMP.pm#get_table()_-_retrieve_a_table_from_the_remote_agent I'm not familiar with SNMP at all, but I would be amazed if there isn't already something to do what you want. http://cpansearch.perl.org/src/DTOWN/Net-SNMP-5.2.0/examples/table.pl
|
# ? Sep 16, 2013 23:32 |
|
That's the reverse of what Im doing, Net::SNMP is used for making an SNMP request to an SNMP handler on a remote server, NetSNMP::Agent is for extending your server's SNMP implementation with a handler you write in Perl. The code I'm writing there is for answering SNMP requests sent to a daemon, not requesting SNMP info from a remote device.
|
# ? Sep 16, 2013 23:58 |
|
I wrote a pure-perl SNMP agent a few years ago for regression testing at my job, boss gave the ok to sling some code your way. I use some Perl decoding code found in mrtg that seems to have fallen out from its official place on the internet, however. I'll send you a PM and maybe upload this stuff in a more formalised sense to CPAN.
|
# ? Sep 17, 2013 02:47 |
|
Okay, I really need some help understanding how regular expressions work. I have this script: http://pastebin.com/Y0ZjfkD3 And it gets a string from a text file. It's a sequence of nucleotides. I'm trying to write some code that notifies the user if there are characters other than A, T, C, or G in the string (which there should be). I tried doing it like this: Perl code:
Do I understand how to use regex correctly? Or am I approaching this the wrong way?
|
# ? Nov 12, 2013 23:32 |
|
The Perl code:
http://ideone.com/FdwtF1 I'd suggest using the following instead, including a handy basic line count so you can pinpoint the location, and I corrected uc(data) to uc($data): Perl code:
Perl code:
Perl code:
Rohaq fucked around with this message at 01:07 on Nov 13, 2013 |
# ? Nov 13, 2013 00:21 |
|
That's more or less what I'm trying to do, thanks. The whole thing about AGTC is that there's characters like N, n, *, and - in it. Anyway, adding last; makes the whole thing stop on the first line with non-AGTC characters. Does Perl not have a "break" keyword or...?
|
# ? Nov 13, 2013 01:47 |
|
Pollyanna posted:That's more or less what I'm trying to do, thanks. The whole thing about AGTC is that there's characters like N, n, *, and - in it. Anyway, adding last; makes the whole thing stop on the first line with non-AGTC characters. Does Perl not have a "break" keyword or...? Break from what? The "last" shown to you by Rohaq will break from the loop. "exit" will break from the program.
|
# ? Nov 13, 2013 02:26 |
|
Anaconda Rifle posted:Break from what? The "last" shown to you by Rohaq will break from the loop. "exit" will break from the program. That's odd, cause with the "last;" there I get: code:
|
# ? Nov 13, 2013 02:58 |
|
That's because it's finding a non-AGTC character on the first line, printing that warning message, then breaking out of the loop. Check the first line of your file, do you have any whitespace characters between sequences on the same line? Can you show us an example line and tell us which characters you expect to see and what an example of bad data is? Edit: If your files are in fasta format then you need to ignore the first line as it contains a description of the sequence. ephphatha fucked around with this message at 03:08 on Nov 13, 2013 |
# ? Nov 13, 2013 03:06 |
|
This is the typical structure of a FASTA file:code:
Basically, what I want to do is eliminate the first line and join the rest together. From there, find the first occurrence of ATG and then from then on each three characters are considered a single unit called a "codon". (This is to prevent frame shifting.) These codons are then compared to a hash table for translation until any four of the "stop" codons are found, at which point the program prints the translation so far and terminates. This should result in this: code:
code:
|
# ? Nov 13, 2013 03:20 |
|
The <> operator reads from the specified file until it finds a newline character (or EOF), returning everything it's read so far including the newline. So if you want to skip the first line, either call <MYFILE> before the while loop, or better yet check if the line starts with the '>' character (and if so skip it by calling 'next;') since all fasta files seem to use that as a marker. The chomp command is needed because <> returns the line with the newline character on the end. Chomp drops the trailing newline from the provided string (if the string does not end with a newline it's a no-op), modifying the string in place. At this point you can append the chomped line to another variable and let the loop run again, it'll fetch another line, drop the newline character, you append it to the same buffer variable, and repeat. At the end of the loop you will have a single line of text you can process. You can then use the index function to find the first occurrence of the substring 'ATG' in the string, and use the return value from that function in the substr function to grab the remainder of the string. You can then use split to break the string up into a list of 3 letter strings (by providing the pattern /[[:alpha:]]{3}/) and then feed the list into join with ' ' as the expression to end up with a single string with space delimited three letter sequences. However, for your purposes you may not want to even join the list back into a single string at that point, it might be easier to iterate over the list and use each three letter sequence in your lookup. ephphatha fucked around with this message at 01:02 on Nov 14, 2013 |
# ? Nov 13, 2013 04:59 |
|
Use File::Slurp if you want to suck up a whole file in one go. It simplifies things.
|
# ? Nov 13, 2013 11:31 |
|
Cool! Thanks guys, I'm almost done. There's one more question I have: how do I return the value of a hash? Like, in Python you would go translation = hash[string], but I don't see a version of that for Perl.
|
# ? Nov 13, 2013 18:53 |
|
Pollyanna posted:Cool! Thanks guys, I'm almost done. There's one more question I have: how do I return the value of a hash? Like, in Python you would go translation = hash[string], but I don't see a version of that for Perl. code:
|
# ? Nov 13, 2013 19:07 |
|
Dammit, that makes sense. Thanks. Another problem: the "last" keyword doesn't work the way I think it does. In this snippet: code:
|
# ? Nov 13, 2013 19:42 |
|
last() works fine, your logic just isn't sound and your use of regexes is just plain broken. Here's a version of the code that should work right. code:
Lastly, you'd serve yourself well by downloading Modern Perl and giving it a read. It will only take you an evening: http://modernperlbooks.com/books/modern_perl/ After that you could also read some more stuff from http://perl-tutorial.org but reading Modern Perl is important.
|
# ? Nov 13, 2013 19:58 |
|
Mithaldu posted:last() works fine, your logic just isn't sound and your use of regexes is just plain broken. Here's a version of the code that should work right. Unfortunately, I don't think that works. The code continues to translate every acid past any of the stop codons and $translation never gets printed. I think the next if statement prevents the last two lines in TRANSLATING from executing if a stop codon is found. What is it supposed to do? If it's supposed to move onto the next codon, then I think the regex is incorrect somehow. Also, what do you mean by my quotes being wrong? I'll read Modern Perl tonight, thanks. This'll make learning the language less of a headache.
|
# ? Nov 13, 2013 20:11 |
|
Pollyanna posted:Also, what do you mean by my quotes being wrong? The quotes inside the regexes. They should only be there if there are quotes in the strings in @codonarray. Also, to make this easier, can you describe your stop condition in english?
|
# ? Nov 13, 2013 20:46 |
|
prefect posted:Use File::Slurp if you want to suck up a whole file in one go. It simplifies things.
|
# ? Nov 13, 2013 20:54 |
|
Mithaldu posted:The quotes inside the regexes. They should only be there if there are quotes in the strings in @codonarray. That was it. It works perfectly now, thanks! How come sometimes regexes need quotes and sometimes they don't?
|
# ? Nov 13, 2013 21:07 |
|
Pollyanna posted:That was it. It works perfectly now, thanks! How come sometimes regexes need quotes and sometimes they don't? It is worth mentioning that you can use numerous surrounding characters after '=~' to denote your regex. Maybe you say code using something like the following: Perl code:
Most people use forward slashes though, and most IDE editors recognise it for syntax highlighting. Here's an example, along with some handy tips for using single/double quotes: Link: http://ideone.com/Bw920G Perl code:
Rohaq fucked around with this message at 21:50 on Nov 13, 2013 |
# ? Nov 13, 2013 21:16 |
|
This is confusing as gently caress. At least I don't have to go \/\/\/\/\/\/\/\/\/\/\/ all the time. Another regex question. I'm trying to find instances of one character being repeated at least 6 times. For this I have: code:
|
# ? Nov 13, 2013 22:42 |
|
Pollyanna posted:This is confusing as gently caress. At least I don't have to go \/\/\/\/\/\/\/\/\/\/\/ all the time. http://codepad.org/3N36zS2R
|
# ? Nov 13, 2013 22:58 |
|
gently caress I'm right and the drat program still doesn't work. Here's what I have right now: Perl code:
edit: Aw, dangit. That "qr" thing needs to be there for this to work. Why? Pollyanna fucked around with this message at 23:19 on Nov 13, 2013 |
# ? Nov 13, 2013 23:02 |
|
|
# ? May 22, 2024 15:11 |
|
Wow. I'd strongly suggest you read that book before doing anything else. Whereever you've been learning Perl, it's been teaching you in the worst possible way.
|
# ? Nov 13, 2013 23:27 |