|
From: David Williams on 2 Oct 2006 12:23 Hello all, I am asking for help with the following code: if($old=~/checksum=(\d+)/) I think the =~ is not equal to meaning if $old (which is a filehandler) is not equal to something. also, checksum, is that a UNIX command? checksum is not a variable anywhere in the code I am debugging. I did a man page on checksum and got nothing back. Lastly, what is \d+ ? The PERL book says \d is digit but I don't understand. Thanks for any help! David -- David Williams Georgia Institute of Technology, Atlanta Georgia, 30332 Email: dw149(a)prism.gatech.edu
From: Paul Lalli on 2 Oct 2006 12:33 David Williams wrote: > I am asking for help with the following code: > > > if($old=~/checksum=(\d+)/) > > I think the =~ is not equal to No, that's not correct. =~ is the binding operator. It says to look for a pattern (the right argument) within the string (the left argument). The "not equal to" operator is != for numbers, and ne for strings > meaning if > $old (which is a filehandler) is not equal to something. If $old is a filehandle, then you're not going to be able to do any useful comparisons on it. You must read a string from the filehandle and do your comparison or pattern matching on that string. You generally do this with the < > operator, like so: my $line = <$old>; > also, checksum, is that a UNIX command? checksum is not a variable > anywhere in the code I am debugging. I did a man page on checksum > and got nothing back. In the above code, 'checksum' is simply a part of the pattern that is being searched for in the variable $old. It is not a variable nor a UNIX command. > Lastly, what is \d+ ? The PERL book says \d is digit but I don't > understand. \d+ is a regular expression token that means "1 or more of any digit". The entirety of the code above says: "if the string contained in $old contains the string 'checksum', followed by an equals sign, followed by 1 or more of any digit, then..." (it also stores the digits that it found in the variable $1, if the pattern is actually successful). You can read more about regular expressions by typing these into your command window: perldoc perlretut perldoc perlre perldoc perlreref And you can find all the operators and what they mean by typing: perldoc perlop Hope this helps, Paul Lalli
From: David Squire on 2 Oct 2006 12:35 David Williams wrote: > Hello all, > I am asking for help with the following code: > > > if($old=~/checksum=(\d+)/) > > I think the =~ is not equal to meaning if > $old (which is a filehandler) is not equal to something. No. It is an operator that associates a string with a regular expression for matching. > > also, checksum, is that a UNIX command? checksum is not a variable > anywhere in the code I am debugging. I did a man page on checksum > and got nothing back. That's because it is part of the regular expression pattern in the match in that line. It's a literal pattern of characters. > Lastly, what is \d+ ? The PERL There's no such language as "PERL". It's Perl. > book says \d is digit but I don't > understand. You need to go back to your book, or to the documentation that comes with Perl, and learn the basics about regular expressions and the Perl operators that use them. See, for example: perldoc perlretut DS
From: Ian Wilson on 2 Oct 2006 12:47 David Williams wrote: > Hello all, > I am asking for help with the following code: I recommend you read an introductory book such as "Learning Perl". You may like to review what is available at http://learn.perl.org Also, at a command prompt you can review the doumentation included with perl - e.g. view the table of contents using the command `perldoc perltoc` > if($old=~/checksum=(\d+)/) > > I think the =~ is not equal to meaning if > $old (which is a filehandler) is not equal to something. I find it's not worth guessing blindly, reading the documentation works well for me. Start at `perldoc perlop` and look for "Binding Operators" > > also, checksum, is that a UNIX command? checksum is not a variable > anywhere in the code I am debugging. I did a man page on checksum > and got nothing back. Your "checksum=" is fixed text in a Perl regular expression. It is something that is being searched for in the contents of the variable $old. In your case $old presumably contains something like "lorem ipsum checksum=3489589713485 dolor sit amet" and your program needs to extract the checksum value. > > Lastly, what is \d+ ? The PERL book says \d is digit but I don't > understand. \d matches "0", "1" ... "8" or "9" + means match one or more of the previous character (\d+) 'captures' a sequence of one or more digits, for example an integer such as "6" or "1238". Capturing means that perl stores the matched text in a special variable for you to use later. > > Thanks for any help! Please follow the references before posting more questions of this sort.
From: anno4000 on 2 Oct 2006 12:48
David Williams <dw149(a)acmex.gatech.edu> wrote in comp.lang.perl.misc: > Hello all, > I am asking for help with the following code: > > > if($old=~/checksum=(\d+)/) > > I think the =~ is not equal to meaning if No. "Not equal" can be expressed as "!=" or "ne" in Perl. The "=~" you have here is a binding operator. In this case it means to match the string $old against the pattern (/checksum=(\d+)/) on the right side. > $old (which is a filehandler) is not equal to something. It doesn't make sense for $old to be a filehandle (not filehander). The pattern match that happens is only useful with a string. > also, checksum, is that a UNIX command? checksum is not a variable > anywhere in the code I am debugging. I did a man page on checksum > and got nothing back. It may or may not be a Unix command, that doesn't matter. In your code it is just part of the pattern to match. > Lastly, what is \d+ ? The PERL book says \d is digit but I don't > understand. You need to understand regular expressions, which are Perl's (and many other languages') way of expressing string patterns. The specific pattern /checksum=(\d+)/ matches any string that contains the characters "checksum=" immediately followed by one or more digits. "hihi haha checksum=123 hoho" would be an example. > Thanks for any help! You won't be able to debug a Perl program (even a short one) with ad-hoc explanations given on Usenet. You'll either have to learn enough Perl to understand the program or get someone else to do it. Anno |