|
Triple Tech posted:Yes, I'm talking to checkeredshawn. So, now we know what the data looks like. What exactly are you trying to determine based off of this data? Are you picking out special elements? Are you deleting duplicates? Are you... doing something? Not really doing much with it right now other than printing, and that all happens within the nested foreach loops I posted. I'm just printing out the results of the comparison. This would be a typical output: firstserver returns the same result(s) as secondserver secondserver returns the same result(s) as firstserver or if they're different firstserver differs from secondserver secondserver differs from firstserver
|
# ? Jul 17, 2008 18:17 |
|
|
# ? May 21, 2024 17:47 |
|
What about creating a new hash where each key is the DNS server value from the previous hash and each value is an array of keys from the previous hash that had that DNS server? Something like this: code:
And then you can do something like: code:
|
# ? Jul 17, 2008 20:09 |
|
I appreciate the advice, Erasmus Darwin, but to clarify, I'm trying to shorten the script. I figured I could find some module that would help me better compare the data, but I've failed to find any such module through cpan.
|
# ? Jul 17, 2008 20:13 |
|
checkeredshawn posted:I appreciate the advice, Erasmus Darwin, but to clarify, I'm trying to shorten the script. I figured I could find some module that would help me better compare the data, but I've failed to find any such module through cpan. Shorter? This is only three lines and creates a structure with the comparisons you want... code:
|
# ? Jul 18, 2008 00:21 |
|
genericadmin posted:
wat isn't that the same as values(%compare)?
|
# ? Jul 18, 2008 07:30 |
|
TiMBuS posted:wat It is. I had @compare{keys(%different_hash)} there before and changed it. But values would be better because it is faster than a slice.
|
# ? Jul 18, 2008 15:34 |
|
I have this code which queries the root name servers for the NS records of a domain (nice and concise unlike dig +trace or whois records).code:
1. Can someone tell me what $response->authority actually is? Printed as string, it looks like Net::DNS::RR::A=HASH(0x9060438) Net::DNS::RR::A=HASH(0x90604a4)... which doesn't seem to be an Array or reference to one. 2. And why does the syntax I'm using the reference the elements in the array work (just using parentheses instead of something like @{})? 3. In line with the previous questions, what are the alternative ways to get the scalar value of it (instead of using int())? 4. Instead of using $a, can I somehow embed $_->name into the regular expression? Comments on anything else in the program are appreciated as well. xobofni fucked around with this message at 19:07 on Jul 20, 2008 |
# ? Jul 20, 2008 19:05 |
|
xobofni posted:1. Can someone tell me what $response->authority actually is? Printed as string, it looks like Net:NS::RR::A=HASH(0x9060438) Net:NS::RR::A=HASH(0x90604a4)... which doesn't seem to be an Array or reference to one. 1. It's an object* that you're trying to look at in scalar context, i.e. you're not interfacing with it as intended. * There are no real objects in Perl, just a system of blessed hashes. 2. Because the slice notation works fine for lists. You could say print +(1, 2, 3)[0] and it prints 1 (the plus sign is unrelated but required for this example). 3. Not even sure what you mean. 4. Probably, but you shouldn't, for the sake of clarity.
|
# ? Jul 20, 2008 19:24 |
|
Triple Tech posted:1. It's an object* that you're trying to look at in scalar context, i.e. you're not interfacing with it as intended. * There are no real objects in Perl, just a system of blessed hashes. Regarding question #1 you may want to try dumping the object since it may help you visualize it better. Use Data::Dumper; <code...> $response = $resolver->send($domain); print Dumper $response;
|
# ? Jul 20, 2008 23:00 |
|
To expand on what Triple Tech wrote...xobofni posted:1. Can someone tell me what $response->authority actually is? Printed as string, it looks like Net::DNS::RR::A=HASH(0x9060438) Net::DNS::RR::A=HASH(0x90604a4)... which doesn't seem to be an Array or reference to one. The authority method returns an array of Net::DNS::RR objects, and you are correctly accessing the first element with [0]. I think you're confused because of how you printed out the return value from authority, since printing out the contents of an array prints out each of its elements as a string without directly telling you that you printed an array. xobofni posted:2. And why does the syntax I'm using the reference the elements in the array work (just using parentheses instead of something like @{})? That syntax is used for array references. You're working with an array here, not an array reference, so that syntax is not needed. xobofni posted:3. In line with the previous questions, what are the alternative ways to get the scalar value of it (instead of using int())? You don't need to use int there, the if should work fine without it. In general, if you want the length of an array, you should use the scalar function. Using int forces the returned array into scalar context (which is the array's length), and the converts it to an integer (which it already is). Using int does the same thing as using scalar but with an extra layer of indirection (and confusion).
|
# ? Jul 20, 2008 23:08 |
|
Thanks for the replies
|
# ? Jul 21, 2008 04:46 |
|
Hi, I'm a major beginner, just finished Beginning Perl and am working on one of my first programs. So I have this code for one of the functions, that deals with a long file of 3 digit numbers (area codes to be specific) It's meant to loop through a data file, which very simple: just area codes, seperated by spaces. This is the code I have so far. code:
|
# ? Jul 21, 2008 05:22 |
|
Seperated by spaces or new lines? The <> operator only seperates automatically by new lines. So, if you want by spaces, you're going to have to cut up each line of numbers first, and then pump your counter algo into it, which on the surface looks sound. Does the output say something like "732 201 212 973 appeared 1 times."
|
# ? Jul 21, 2008 05:51 |
|
Triple Tech posted:So, if you want by spaces, you're going to have to cut up each line of numbers first, and then pump your counter algo into it, which on the surface looks sound. In which case, the 'split' function is probably what you want. You feed it a regexp and a string, and it splits the string into an array of strings using the regexp as the separator. For example: split /:/, 'foo:bar:baz' would return [ 'foo', 'bar', 'baz' ]. (Note that the separator, ':' in this case, is completely removed from the output.) Also, since the output of split is an array, you can quickly iterate through it using a foreach loop. Here's one way to write the main read/processing loop (in spoiler tags because it's worth trying to figure it out on your own first): code:
code:
Also, there are other ways to do it besides using split. For example, you can use a loop that iterates on the regexp operator with the 'g' flag in order to have it return every 3 digit block in a given string. That works better if your file's not in a consistent format or if there's a lot of stuff you're skipping over. For example, that'd be the way to go if you wanted to count every area code embedded in a bunch of English text talking about the joys of area codes.
|
# ? Jul 21, 2008 14:16 |
|
Triple Tech posted:Seperated by spaces or new lines? The <> operator only seperates automatically by new lines. So, if you want by spaces, you're going to have to cut up each line of numbers first, and then pump your counter algo into it, which on the surface looks sound. You're right, I went into my other script that grabbed the data and had it add newlines and everything works now. Thanks.
|
# ? Jul 21, 2008 18:13 |
|
This is a perfect time to do something like:code:
(Although, if performance was an issue, it would be better to read from the file 4 bytes at a time (3 for the area code + 1 for the trailing space) and chop the result. Let the OS handle the prefetching by specifying the correct flags to sysopen.)
|
# ? Jul 21, 2008 23:35 |
|
code:
EDIT: html_generate just sets the HTML::Template variables and template page and returns the output Heres DB_get_trades just incase i'm returning things wrong... code:
|
# ? Jul 22, 2008 05:35 |
|
uG posted:
Does this code run under use strict? Where is %resource_hash defined? Are you sure you don't mean to push $resource_hash instead?
|
# ? Jul 22, 2008 09:31 |
|
Ninja Rope posted:Does this code run under use strict? Where is %resource_hash defined? Are you sure you don't mean to push $resource_hash instead? It runs under strict, and instead of pushing %resource_hash I think I should be pushing $resource_hash. Unfortunately that is not the problem EDIT: Problem solved. Just needed some sleep and a clear mind uG fucked around with this message at 18:27 on Jul 22, 2008 |
# ? Jul 22, 2008 18:01 |
|
...
maskenfreiheit fucked around with this message at 04:22 on Sep 29, 2010 |
# ? Jul 23, 2008 01:27 |
|
Why are you using my $hash{key}? Just remove the mys.
|
# ? Jul 23, 2008 01:31 |
|
...
maskenfreiheit fucked around with this message at 04:23 on Sep 29, 2010 |
# ? Jul 23, 2008 01:42 |
|
The data structure must exist outside the scope of the Evaluate function. Just collapse the ThreeDigitMost subroutine into regular code and put the Evaluate definition all the way at the bottom.
|
# ? Jul 23, 2008 01:50 |
|
...
maskenfreiheit fucked around with this message at 04:23 on Sep 29, 2010 |
# ? Jul 23, 2008 02:08 |
|
You could just do this:code:
code:
tef fucked around with this message at 02:19 on Jul 23, 2008 |
# ? Jul 23, 2008 02:15 |
|
Add use warnings; right before use strict; or you're fired. Out of a cannon. Into the sun.
|
# ? Jul 23, 2008 05:24 |
|
Thanks to strict.pm's 33% stacking combo against skeletons you're better off loading it first, then warnings. But hey, whatever works for you. In the meantime the horrible code I'm paid to stop spontaneously combusting all the time is driving me insane, how do you guys mentally cope when dealing with really bad code all day, every day? Mario Incandenza fucked around with this message at 15:14 on Jul 23, 2008 |
# ? Jul 23, 2008 15:08 |
|
Ninja Rope posted:Add use warnings; right before use strict; or you're fired. Out of a cannon. Into the sun. I thought the generally accepted canon was code:
|
# ? Jul 23, 2008 16:55 |
|
perl -w enables warnings globally, which you might not want. use warnings is lexical, and only affects the current file/sub/block.
|
# ? Jul 23, 2008 17:08 |
|
SpeedFrog posted:perl -w enables warnings globally, which you might not want. use warnings is lexical, and only affects the current file/sub/block. Ah, good point.
|
# ? Jul 23, 2008 17:08 |
|
Also not all platforms (Windows) respect the shebang.
|
# ? Jul 23, 2008 17:16 |
|
Triple Tech posted:Also not all platforms (Windows) respect the shebang. Windows obviously doesn't use the shebang to invoke perl, but perl on Windows (or at least the copy of ActivePerl that I've got installed here) is smart enough to manually parse the shebang line once it's invoked. So "#!/usr/bin/perl -w" still works on Windows.
|
# ? Jul 23, 2008 18:36 |
|
Is there a way to see how much Perl I know? Or how much I stack up against the competition? I'm reading this job posting and wonder if I qualify. I probably don't. I've heard any of the following are metrics for god-like Perl programmers: • contributing to perl itself (5 or 6) • working with XS • publishing to CPAN I've done nothing of the sort. Am I not as good as I think I am?
|
# ? Jul 23, 2008 19:35 |
|
Triple Tech posted:Am I not as good as I think I am? Yes. I would expect the top 1% of perl hackers to understand lexical scope.
|
# ? Jul 23, 2008 19:57 |
|
I used to be handy enough with Perl that I could probably hack my way to a solution for this problem in a relatively short amount of time, but what little skill I had has atrophied since I moved out of development. I've got files named foo1.jpg, foo2.jpg, ... foo521.jpg No, they're not porn. I just want to rename them with the integer part of the filename padded with zeros (foo001.jpg, foo002.jpg, ...). Given that this can probably be done with about three lines of Perl, can anyone offer a quick solution? Google wasn't very helpful, or maybe I just suck at crafting searches.
|
# ? Jul 27, 2008 04:59 |
|
GroovinPickle posted:I've got files named foo1.jpg, foo2.jpg, ... foo521.jpg First, you suck for posting this in the Perl thread. If you have the rename perl script: rename 's/\d+/sprintf "%03d", $&/' foo*.jpg Otherwise, perl -e 'for (@ARGV) { my $old = $_; s/\d+/sprintf "%03d", $&/; rename $old => $_ }' foo*.jpg
|
# ? Jul 27, 2008 05:05 |
|
This worked for me: rename 's/\d+/sprintf "%03d", $&/e' foo*.jpg
|
# ? Jul 27, 2008 05:43 |
|
Ah. Good catch. Sorry.
|
# ? Jul 27, 2008 05:51 |
|
/folder/ /folder/number /folder/number/filename Is there a regex that will match the above 3 strings, but will capture number and filename *if* they exist?
|
# ? Jul 28, 2008 07:02 |
|
|
# ? May 21, 2024 17:47 |
|
uG posted:/folder/ Here ya go. quote:# for i in "/folder/" "/folder/number" "/folder/number/filename"; do echo -e "\n=== $i"; echo $i | perl -ne 'chomp;/\/[^\/]*\/([^\/]*)[\/]*(.*)/; print "num = $1\nfilename = $2\n";'; done Might add some more specific character set matching for the "number" and "filename," but not really necessary.
|
# ? Jul 28, 2008 08:06 |