perl default variable question
hi, i'm using a perl script i found to change the names of batches of files. The program works as is but it's giving me a weird warning. I'm familiar with $_, but not $_[3]. Can someone explain what $_[3] is, and how i can get this script to stop throwning the warning? Thanks, Ryan i found the file here: http://noisybox.net/computers/eren/ file download link: http://noisybox.net/computers/eren/eren.pl warning thrown: Use of uninitialized value in pattern match (m//) at C:\pPerl\eren.pl line 82. (i changed the file a bit so it won't be the same line number if you checkout the whole file) code from my file: 76 foreach my $file (@files){ 77 next if -d($root . $file); 78 79 next if (($preview) and not ($file =~ /$filefilter/)); 80 @_ = split /\//, $replacestr; 81 my $icase = ''; 82 ($_[3] =~ /i/) and $icase = 'i'; #** 83 my $cmd = sprintf(\$file =~ /%s/%s;, $_[1], $icase); 84 85 next if not eval $cmd; 86 87 $preview and print PREVIEW: ; 88 $matchct++; 89 90 my $oldfile = $file; 91 print $oldfile -- ; 92 $file =~ eval \$file =~ s$replacestr;; 93 print $file\n; 94 $preview and next; 95 rename($root . $oldfile, $root . $file) or print Failed to rename $root$oldfile\n; 96 } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: perl default variable question
On 9/18/07, Ryan Moszynski [EMAIL PROTECTED] wrote: i'm using a perl script i found to change the names of batches of files. The program works as is but it's giving me a weird warning. I'm familiar with $_, but not $_[3]. Can someone explain what $_[3] is, and how i can get this script to stop throwning the warning? 80 @_ = split /\//, $replacestr; 81 my $icase = ''; 82 ($_[3] =~ /i/) and $icase = 'i'; $_[3] is the fourth element in the @_ array. Although that array is normally used for the subroutine parameter list, it seems that this programmer has used it to hold something else. (It would be better style to use a different variable, or even a list of well-named scalars. Splitting into @_ was somewhat common in Perl's ancient history, though; maybe this script was written more than ten years ago?) In any case, the error's source seems to be that @_ doesn't have at least four items, I'd guess because $replacestr didn't have at least three forward slashes and then some text. 83 my $cmd = sprintf(\$file =~ /%s/%s;, $_[1], $icase); 84 85 next if not eval $cmd; But since this code is using the evil eval, I'm confident that you can write a better (perhaps safer) program from scratch. It looks as if the programmer didn't know that you don't have to use the evil eval in order to be able to choose case-insensitive matches at run-time. And maybe the programmer didn't know that Perl can interpolate into double-quoted strings? sprintf isn't needed either. my $insensitive = $icase ? '(?:i)' : ''; next if not $file =~ /$insensitive$_[1]/; Good luck with it! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: perl default variable question
On 9/18/07, Ryan Moszynski [EMAIL PROTECTED] wrote: thanks, so i can just test to see if $_[3] exists and it kills the warning. On 9/18/07, Andrew Curry [EMAIL PROTECTED] wrote: @_ is the list of incoming parameters to a sub. So if you write a sub, you refer to the first parameter in it as $_[0], the second parameter as $_[1] and so on. And you can refer to $_# as the index number of the last parameter: -Original Message- From: Ryan Moszynski [mailto:[EMAIL PROTECTED] Sent: 18 September 2007 15:12 To: beginners@perl.org Subject: perl default variable question hi, i'm using a perl script i found to change the names of batches of files. The program works as is but it's giving me a weird warning. I'm familiar with $_, but not $_[3]. Can someone explain what $_[3] is, and how i can get this script to stop throwning the warning? Thanks, Ryan i found the file here: http://noisybox.net/computers/eren/ file download link: http://noisybox.net/computers/eren/eren.pl warning thrown: Use of uninitialized value in pattern match (m//) at C:\pPerl\eren.pl line 82. (i changed the file a bit so it won't be the same line number if you checkout the whole file) code from my file: 76 foreach my $file (@files){ 77 next if -d($root . $file); 78 79 next if (($preview) and not ($file =~ /$filefilter/)); 80 @_ = split /\//, $replacestr; 81 my $icase = ''; 82 ($_[3] =~ /i/) and $icase = 'i'; #** 83 my $cmd = sprintf(\$file =~ /%s/%s;, $_[1], $icase); 84 85 next if not eval $cmd; 86 87 $preview and print PREVIEW: ; 88 $matchct++; 89 90 my $oldfile = $file; 91 print $oldfile -- ; 92 $file =~ eval \$file =~ s$replacestr;; 93 print $file\n; 94 $preview and next; 95 rename($root . $oldfile, $root . $file) or print Failed to rename $root$oldfile\n; 96 } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ This e-mail is from the PA Group. For more information, see www.thepagroup.com. This e-mail may contain confidential information. Only the addressee is permitted to read, copy, distribute or otherwise use this email or any attachments. If you have received it in error, please contact the sender immediately. Any opinion expressed in this e-mail is personal to the sender and may not reflect the opinion of the PA Group. Any e-mail reply to this address may be subject to interception or monitoring for operational reasons or for lawful business practices. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: perl default variable question
:-) $_ is basically the first variable of @_ which is the array that Perl is currently working on. So when your script is complaining about something in $_[3] what it is saying is that the 4th variable (Perl starts counting at 0) in the @_ array is making the compiler unhappy. I hope this helps a bit. Look at line 83 of your code as you are using $_[3] in comparison, but $_[3] might be undefined which will cause your interpeter to scream at you. On 9/18/07, Ryan Moszynski [EMAIL PROTECTED] wrote: hi, i'm using a perl script i found to change the names of batches of files. The program works as is but it's giving me a weird warning. I'm familiar with $_, but not $_[3]. Can someone explain what $_[3] is, and how i can get this script to stop throwning the warning? Thanks, Ryan i found the file here: http://noisybox.net/computers/eren/ file download link: http://noisybox.net/computers/eren/eren.pl warning thrown: Use of uninitialized value in pattern match (m//) at C:\pPerl\eren.pl line 82. (i changed the file a bit so it won't be the same line number if you checkout the whole file) code from my file: 76 foreach my $file (@files){ 77 next if -d($root . $file); 78 79 next if (($preview) and not ($file =~ /$filefilter/)); 80 @_ = split /\//, $replacestr; 81 my $icase = ''; 82 ($_[3] =~ /i/) and $icase = 'i'; #** 83 my $cmd = sprintf(\$file =~ /%s/%s;, $_[1], $icase); 84 85 next if not eval $cmd; 86 87 $preview and print PREVIEW: ; 88 $matchct++; 89 90 my $oldfile = $file; 91 print $oldfile -- ; 92 $file =~ eval \$file =~ s$replacestr;; 93 print $file\n; 94 $preview and next; 95 rename($root . $oldfile, $root . $file) or print Failed to rename $root$oldfile\n; 96 } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: perl default variable question
Ryan Moszynski wrote: hi, Hello, i'm using a perl script i found You should be careful with stuff you find lying around. to change the names of batches of files. The program works as is but it's giving me a weird warning. I'm familiar with $_, but not $_[3]. Can someone explain what $_[3] is, and how i can get this script to stop throwning the warning? $_[3] is the fourth element of the array @_. i found the file here: http://noisybox.net/computers/eren/ file download link: http://noisybox.net/computers/eren/eren.pl Did you read the second line of that file? # this program sucks and is a total hack and needs TONS of work... warning thrown: Use of uninitialized value in pattern match (m//) at C:\pPerl\eren.pl line 82. This means that the fourth element of the array @_ contains the value undef. (i changed the file a bit so it won't be the same line number if you checkout the whole file) code from my file: 76 foreach my $file (@files){ 77 next if -d($root . $file); 78 79 next if (($preview) and not ($file =~ /$filefilter/)); 80 @_ = split /\//, $replacestr; 81 my $icase = ''; 82 ($_[3] =~ /i/) and $icase = 'i'; #** 83 my $cmd = sprintf(\$file =~ /%s/%s;, $_[1], $icase); 84 85 next if not eval $cmd; As the comment says, this sucks. You can accomplish the same thing without using eval. 86 87 $preview and print PREVIEW: ; 88 $matchct++; 89 90 my $oldfile = $file; 91 print $oldfile -- ; 92 $file =~ eval \$file =~ s$replacestr;; 93 print $file\n; 94 $preview and next; 95 rename($root . $oldfile, $root . $file) or print Failed to rename $root$oldfile\n; 96 } John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
$_ variable question
Friends: I have an outerloop with a list and so do I have an inner loop with another list. $_ variable points to list in the outer-loop or inner-loop depending upon the scope. I prefer to not use aliases. In such a case, when I am in the scope of inner loop, can I access the looping variable on the outer without using an explicit alias variable? For ex: __ Do You Yahoo!? Yahoo! Finance - Get real-time stock quotes http://finance.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
$_ variable question
Friends: I have an outerloop with a list and so do I have an inner loop with another list. $_ variable points to list in the outer-loop or inner-loop depending upon the scope. I prefer to not use aliases. In such a case, when I am in the scope of inner loop, can I access the looping variable on the outer without using an explicit alias variable? For ex: for(1..10){ print($_\n\t); # $_ is 1,2,3... for(a..h){ print($_\t); # $_ is a,b,c... # How can I refer to 1,2,3 here?? } print(\n); } __ Do You Yahoo!? Yahoo! Finance - Get real-time stock quotes http://finance.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: $_ variable question
You can't, exactly. You have just overwritten $_ with the second loop. The only way you COULD do this is maybe by declaring $_ with local() somehow? I don't know. Even if you could figure out how to do that, however, you would be doing the same thing as creating a new variable, only you will be making it a lot more complicated than it has to be. If I were you (which, understandably, I am not), I would stick with: for(1..10){ print $_\n\t; foreach my $inner(a..h){ print $inner\t; } print \n; } -Original Message- From: RTO RTO [mailto:[EMAIL PROTECTED]] Sent: Monday, September 09, 2002 2:33 PM To: [EMAIL PROTECTED] Subject: $_ variable question Friends: I have an outerloop with a list and so do I have an inner loop with another list. $_ variable points to list in the outer-loop or inner-loop depending upon the scope. I prefer to not use aliases. In such a case, when I am in the scope of inner loop, can I access the looping variable on the outer without using an explicit alias variable? For ex: for(1..10){ print($_\n\t); # $_ is 1,2,3... for(a..h){ print($_\t); # $_ is a,b,c... # How can I refer to 1,2,3 here?? } print(\n); } __ Do You Yahoo!? Yahoo! Finance - Get real-time stock quotes http://finance.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Fwd: RE: $_ variable question
Note: forwarded message attached. __ Do You Yahoo!? Yahoo! Finance - Get real-time stock quotes http://finance.yahoo.com ---BeginMessage--- Tim -- Thanks for your rejoinder. Mostly, I do use 'aliased' variables within nested loops. However, this question came up in my mind serendipitiously when I was working on one such instance, without aliasing. I just wanted to run by the Perl cognoscenti to make sure that the answer to my question was in the negative. The question was purely academic in its interest (that is why, I had wantonly mentioned without using aliasing). Thanks, once again!!! Cheers, Rex --- Timothy Johnson [EMAIL PROTECTED] wrote: You can't, exactly. You have just overwritten $_ with the second loop. The only way you COULD do this is maybe by declaring $_ with local() somehow? I don't know. Even if you could figure out how to do that, however, you would be doing the same thing as creating a new variable, only you will be making it a lot more complicated than it has to be. If I were you (which, understandably, I am not), I would stick with: for(1..10){ print $_\n\t; foreach my $inner(a..h){ print $inner\t; } print \n; } __ Do You Yahoo!? Yahoo! Finance - Get real-time stock quotes http://finance.yahoo.com ---End Message--- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: $_ variable question
On Mon, Sep 09, 2002 at 02:29:24PM -0700, RTO RTO wrote: $_ variable points to list in the outer-loop or inner-loop depending upon the scope. I prefer to not use aliases. In such a case, when I am in the scope of inner loop, can I access the looping variable on the outer without using an explicit alias variable? No. You have effectively hidden the previous value of $_ in the inner loop, and there is no way currently of getting to it again. By alias I assume you mean a named iterator, i.e.: foreach my $foo (0 .. 4) { ... } where $foo is what you're calling an alias. You'll have to use a named iterator for one of the loops. Out of curiousity, why do you prefer not to use a named iterator? They're often more readable than the ever-implicit $_, especially if you end up referring to the variable explicitly in the block. Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: $_ variable question
Michael -- As I had earlier posited, it was just out of curiosity and the question was more in tune with academic curiosity rather than pragmatic correctiveness. I always used to have named iterators, but when I was programming without them today, this question came up to my mind instantaneously. Though I presumed that the chances were low, because of $_ masquerading values as per the scope, I still wanted to make sure for a fact, that it was not possible. All the noetic exchanges on this subject, have clearly demonstrated that it is not possible and one has to use only named iterators. Thanks, Rex Michael Fowler wrote: On Mon, Sep 09, 2002 at 02:29:24PM -0700, RTO RTO wrote: Out of curiousity, why do you prefer not to use a named iterator? They're often more readable than the ever-implicit $_, especially if you end up referring to the variable explicitly in the block. - Do You Yahoo!? Yahoo! Finance - Get real-time stock quotes
RE: Object oriented variable question
You are trying to assign a static value to something that comes from the form. If you create a field called comments (or anything you want to call it, that's just what I chose) on your form and then type your test string, it will work with the following change to your code: == #!/usr/bin/perl -wT use CGI; use CGI::Carp 'fatalsToBrowser'; use strict; my $q = new CGI; my $test = $q-param('comments'); # Dynamic value rather # than a static one print $q-header(text/html), $q-start_html, $q-p(Hello! . $test . Did it work?), $q-end_html; = Now, on your form, type This is a test! into your comments box and see what results you get. Betcha it works this time. If you wanted $test to always have a static value (I'm not sure why you would, but then again I don't know your application), you could have just set my $test = This is a test!; Scot R. inSite Internet Solutions [EMAIL PROTECTED] http://www.insiteful.tv -Original Message- From: Hughes, Andrew [mailto:[EMAIL PROTECTED]] Sent: Monday, June 24, 2002 10:20 AM To: [EMAIL PROTECTED] Subject: Object oriented variable question When using cgi.pm object oriented method, how do I assign a static value to a variable and then output it? The test script that I listed below prints all of the html tags with Hello!Did it work? (without the quotes) in the paragraph tags. Also, in my error log there is the following error: index.cgi: Use of uninitialized value in concatenation (.) or string at index.cgi line 9. == #!/usr/bin/perl -wT use CGI; use CGI::Carp 'fatalsToBrowser'; use strict; my $q = new CGI; my $test = $q-param(This is a test!); print $q-header(text/html), $q-start_html, $q-p(Hello! . $test . Did it work?), $q-end_html; = Thanks for your help! Andrew -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.370 / Virus Database: 205 - Release Date: 6/5/2002 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.370 / Virus Database: 205 - Release Date: 6/5/2002 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Variable question
Hi everyone. I was wondering how you would determine whether a variable is a number or not. I want to do an if statement such as if ($variable is a number) {... Any help is great. Thanx, Helen - Find, Connect, Date! Yahoo! Canada Personals
Re: Variable question
On Wed, Apr 17, 2002 at 02:44:32PM -0400, Helen Dynah wrote: I was wondering how you would determine whether a variable is a number or not. Use a regex, see perldoc -q 'is a number' or http://www.perldoc.com/perl5.6.1/pod/perlfaq4.html, second question in the Data: Misc section. Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Variable question
Here's one way: #\d represents a digit #this regex checks to see if every character (besides possibly a trailing \n) is a digit if($var =~ /^\d+$/){ do something... }else{ don't } -Original Message- From: Helen Dynah [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 17, 2002 11:45 AM To: [EMAIL PROTECTED] Subject: Variable question Hi everyone. I was wondering how you would determine whether a variable is a number or not. I want to do an if statement such as if ($variable is a number) {... Any help is great. Thanx, Helen - Find, Connect, Date! Yahoo! Canada Personals -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
On Wed, 2002-04-10 at 11:10, Randal L. Schwartz wrote: Bob == Bob Ackerman [EMAIL PROTECTED] writes: At no point do you have an array in a scalar context, or a list in a scalar context. Really. You don't. Ever. Get it? And why I'm harping on this is that I've seen this myth continue to perpetuate, started from some bad verbage or bad understanding somewhere, and I'm trying to root it out so that it doesn't keep spreading like a bad meme. Bob oooh. i get it. i thought you were overboard, too - until that Bob last go around. you are right. it is subtle, but important. Thank you thank you thank you thank you! It *is* important. Spread the word. :) -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! Sorry for not responding earlier, but illness and Real Work(tm) intervened. Okay, I get it too. The list assignment operator returns the number items copied across, but that still doesn't change how I read it. I read my $var if 0; as $var is a static variable even though it really isn't. I like to assign tags to idioms in my head; in this case static variable and array() in the case of using an empty list assignment operator to get the number of matches in a regex. I don't consider this a bad thing unless you go overboard in thinking that your tag has real meaning. -- Today is Sweetmorn the 33rd day of Discord in the YOLD 3168 Hail Eris, Hack Linux! Missile Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
Bob == Bob Ackerman [EMAIL PROTECTED] writes: At no point do you have an array in a scalar context, or a list in a scalar context. Really. You don't. Ever. Get it? And why I'm harping on this is that I've seen this myth continue to perpetuate, started from some bad verbage or bad understanding somewhere, and I'm trying to root it out so that it doesn't keep spreading like a bad meme. Bob oooh. i get it. i thought you were overboard, too - until that Bob last go around. you are right. it is subtle, but important. Thank you thank you thank you thank you! It *is* important. Spread the word. :) -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Variable question
I believe it is as simple as: $count = () = $string =~ /,/g; I can't seem to get my brain around what's happening here... would someone be kind enough to explain? -dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
On Monday, April 8, 2002, at 06:24 AM, David Gray wrote: I believe it is as simple as: $count = () = $string =~ /,/g; I can't seem to get my brain around what's happening here... would someone be kind enough to explain? -dave $string =~ /,/g; that finds all occurrences of comma in the variable, $string. assigns the result in a list context - the anonymous list '()'. by assigning this to a scalar, $count, we get a value that is the size of the list, which is the number of matches that the regex made. that empty list thingy is confusing. it is more comprehensible if you assign it to a named array: $count = @arr = $string =~ /,/g; the matches are in @arr which consists of the comma of the match. then $count is the size of the array. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
On Mon, 2002-04-08 at 12:00, bob ackerman wrote: On Monday, April 8, 2002, at 06:24 AM, David Gray wrote: I believe it is as simple as: $count = () = $string =~ /,/g; I can't seem to get my brain around what's happening here... would someone be kind enough to explain? -dave $string =~ /,/g; that finds all occurrences of comma in the variable, $string. assigns the result in a list context - the anonymous list '()'. by assigning this to a scalar, $count, we get a value that is the size of the list, which is the number of matches that the regex made. that empty list thingy is confusing. it is more comprehensible if you assign it to a named array: $count = @arr = $string =~ /,/g; the matches are in @arr which consists of the comma of the match. then $count is the size of the array. With the downside that you have an array that you never use. Using () to force list context is one of those strange little quirks that you just get used to. These days I read () as the array equivalent of scalar(). -- Today is Pungenday the 25th day of Discord in the YOLD 3168 Missile Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
On Monday, April 8, 2002, at 10:40 AM, Chas Owens wrote: On Mon, 2002-04-08 at 12:00, bob ackerman wrote: On Monday, April 8, 2002, at 06:24 AM, David Gray wrote: I believe it is as simple as: $count = () = $string =~ /,/g; I can't seem to get my brain around what's happening here... would someone be kind enough to explain? -dave $string =~ /,/g; that finds all occurrences of comma in the variable, $string. assigns the result in a list context - the anonymous list '()'. by assigning this to a scalar, $count, we get a value that is the size of the list, which is the number of matches that the regex made. that empty list thingy is confusing. it is more comprehensible if you assign it to a named array: $count = @arr = $string =~ /,/g; the matches are in @arr which consists of the comma of the match. then $count is the size of the array. With the downside that you have an array that you never use. Using () to force list context is one of those strange little quirks that you just get used to. These days I read () as the array equivalent of scalar(). agreed. i didn't mean to actually use a named array. i was suggesting it as a way to understand what was happening. definitely no sense to actually name the array in your working code. -- Today is Pungenday the 25th day of Discord in the YOLD 3168 when is the next YOLD holiday? can we take the month off? :) Missile Address: 33:48:3.521N 84:23:34.786W would that missile be armed? is that a US missile or perhaps an Anarchist missile? missive? misdirection? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
On Mon, 2002-04-08 at 14:37, Randal L. Schwartz wrote: Chas == Chas Owens [EMAIL PROTECTED] writes: There is no meaning for list in a scalar context, so your statement makes no sense. Chas my $some_scalar = () = /\s/g; Chas I emphasize again, that is how I _read_ it. I know that there is no Chas array() and I know why, but that doesn't change how I read things. This Chas hack forces the far left hand bit to return as a list (by making Chas wantarray return true) which then gets evaluated in scalar context, No, that's what I'm saying CANNOT EXIST. Yes, you are right, that was a slip of the keyboard, I meant that to say array instead of list. But I must ask if you are being purposefully obtuse. The list assignment operator forces wantarray to to return true which is what most people would want a mythical array() function for in the first place, hence my -- again I stress -- _reading_ of () as array(). I am not claiming that it _is_ array(), but that I find it helpful to _think of it as_ array() in this context. You cannot have a list in a scalar context. You have an array name, or a comma operator, or a list assignment operator, or grep, or a slice, or ... , in a scalar context. But NONE OF THOSE GENERATE A LIST IN A SCALAR CONTEXT. Chas that Chas is what I would want array() for so I simply read () (when used as Chas above) as array(). What you are doing here by adding the () is replacing the right side of a scalar assignment with a list assignment instead of the bare operator. It is this *list assignment* operator when evaluated in a scalar context that returns a single value... defined as the number of elements present on the right. But if list assignment operator in a scalar context had been defined by Larry to be return last value, like a slice, you'd be hosed. Of course, that'd break the idiom while (($k, $v) = each %foo) { ... } for the first false $v, but it'd still mostly work. :) THERE IS NEVER A LIST IN A SCALAR CONTEXT. Get it? -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Today is Pungenday the 25th day of Discord in the YOLD 3168 You are what you see. Missile Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Variable question
$count = () = $string =~ /,/g; $string =~ /,/g; assigns the result in a list context - the anonymous list '()'. by assigning this to a scalar, $count, we get a value that is the size of the list, which is the number of matches that the regex made. that empty list thingy is confusing. it is more comprehensible if you assign it to a named array: $count = @arr = $string =~ /,/g; the matches are in @arr which consists of the comma of the match. then $count is the size of the array. With the downside that you have an array that you never use. Using () to force list context is one of those strange little quirks that you just get used to. These days I read () as the array equivalent of scalar(). agreed. i didn't mean to actually use a named array. i was suggesting it as a way to understand what was happening. definitely no sense to actually name the array in your working code. Question answered, much appreciated. -dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
Chas == Chas Owens [EMAIL PROTECTED] writes: Chas With the downside that you have an array that you never use. Using () Chas to force list context is one of those strange little quirks that you Chas just get used to. These days I read () as the array equivalent of Chas scalar(). Well, there can be no array equivalent of scalar. The only place you would use it is to provide list context to a subexpression where a scalar context is being provided. But then what? You have a list, and you have a scalar needed. Do you want the length (like scalar @a or scalar grep or scalar map or scalar keys)? The first element (scalar assignment)? The last element (comma)? A random element? {grin} The second element (getpwnam)? A single line instead of the entire file, retaining the ability to read the rest of the file (readline)? There is no meaning for list in a scalar context, so your statement makes no sense. That's why there's no keyword array like the keyword scalar. It makes no sense. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
On Monday, April 8, 2002, at 12:15 PM, Randal L. Schwartz wrote: Chas == Chas Owens [EMAIL PROTECTED] writes: Chas I emphasize again, that is how I _read_ it. I know that there is no Chas array() and I know why, but that doesn't change how I read things. This Chas hack forces the far left hand bit to return as a list (by making Chas wantarray return true) which then gets evaluated in scalar context, No, that's what I'm saying CANNOT EXIST. Chas Yes, you are right, that was a slip of the keyboard, I meant that to say Chas array instead of list. But I must ask if you are being purposefully Chas obtuse. The list assignment operator forces wantarray to to return true Chas which is what most people would want a mythical array() function for in Chas the first place, hence my -- again I stress -- _reading_ of () as Chas array(). I am not claiming that it _is_ array(), but that I find it Chas helpful to _think of it as_ array() in this context. No, it's not working that way. It works because of two steps here. The operator ($list, $of, $things) = LIST_CONTEXT forces its right side to list context. Thus, you get the list behavior of /\s/g and it spits out all the matches. Next, the scalar value of the list assignment operator is the number of items copied across. But this is because of the property of a list assignment operator in a scalar context. It's not a list in a scalar context. At no point do you have an array in a scalar context, or a list in a scalar context. Really. You don't. Ever. Get it? And why I'm harping on this is that I've seen this myth continue to perpetuate, started from some bad verbage or bad understanding somewhere, and I'm trying to root it out so that it doesn't keep spreading like a bad meme. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! oooh. i get it. i thought you were overboard, too - until that last go around. you are right. it is subtle, but important. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
Chas == Chas Owens [EMAIL PROTECTED] writes: There is no meaning for list in a scalar context, so your statement makes no sense. Chas my $some_scalar = () = /\s/g; Chas I emphasize again, that is how I _read_ it. I know that there is no Chas array() and I know why, but that doesn't change how I read things. This Chas hack forces the far left hand bit to return as a list (by making Chas wantarray return true) which then gets evaluated in scalar context, No, that's what I'm saying CANNOT EXIST. You cannot have a list in a scalar context. You have an array name, or a comma operator, or a list assignment operator, or grep, or a slice, or ... , in a scalar context. But NONE OF THOSE GENERATE A LIST IN A SCALAR CONTEXT. Chas that Chas is what I would want array() for so I simply read () (when used as Chas above) as array(). What you are doing here by adding the () is replacing the right side of a scalar assignment with a list assignment instead of the bare operator. It is this *list assignment* operator when evaluated in a scalar context that returns a single value... defined as the number of elements present on the right. But if list assignment operator in a scalar context had been defined by Larry to be return last value, like a slice, you'd be hosed. Of course, that'd break the idiom while (($k, $v) = each %foo) { ... } for the first false $v, but it'd still mostly work. :) THERE IS NEVER A LIST IN A SCALAR CONTEXT. Get it? -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
On Sat, Apr 06, 2002 at 11:32:01PM -0800, John W. Krahn wrote: And if you really want to get cute you can put it all on one line: substr( $ARGV[0], $_, 1 ) eq $ARGV[1] and $cnt++ for 0 .. length( $ARGV[0] ) - 1; print $cnt; I count two lines ;-) Both of these are a little obfuscated, but show useful techniques: $ perl -e 'print eval \$ARGV[0] =~ y/$ARGV[1]//' q,w,e,r,t,y , 5 $ perl -e 'print scalar (() = $ARGV[0] =~ /$ARGV[1]/g)' q,w,e,r,t,y , 5 But this is neither fwp nor golf. :-) Quite :-) -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Variable question
Execute the following on cmd line as follows $./cnt.pl string , You can replace the comma (,) on the command line to find any other character's occurrence in the string ___ #!/usr/bin/perl for(;$ilength($ARGV[0]);$i++) { $str[i] = substr($ARGV[0],$i,1); if($str[i] eq $ARGV[1]) { $cnt++; } } print $cnt; --- Aman PS: I am just a novice, so there might be quicker ways I yet don't know of !!! -Original Message- From: Helen Dynah [mailto:[EMAIL PROTECTED]] Sent: Friday, April 05, 2002 11:53 AM To: [EMAIL PROTECTED] Subject: Variable question Hi everybody. I am a new user and my first question to this list is probably a very simple one. I am trying to count the number of commas in a variable. The book I am learning from doesn't cover specific information like that. Thanks for any help. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
Aman Cgiperl wrote: Execute the following on cmd line as follows $./cnt.pl string , You can replace the comma (,) on the command line to find any other character's occurrence in the string ___ #!/usr/bin/perl for(;$ilength($ARGV[0]);$i++) { $str[i] = substr($ARGV[0],$i,1); if($str[i] eq $ARGV[1]) { $cnt++; } } print $cnt; --- Aman PS: I am just a novice, so there might be quicker ways I yet don't know of !!! Hi Aman. No offense but that looks more like C than Perl. Why are you using an array (@str) to store the current character instead of a scalar? for ( ; $i length( $ARGV[0] ); $i++ ) { $str = substr( $ARGV[0], $i, 1 ); if ( $str eq $ARGV[1] ) { $cnt++; } } print $cnt; As a matter of fact, why not just use the substr in the comparison? for ( ; $i length( $ARGV[0] ); $i++ ) { if ( substr( $ARGV[0], $i, 1 ) eq $ARGV[1] ) { $cnt++; } } print $cnt; And of course we can make the for statement more perl-like. for ( 0 .. length( $ARGV[0] ) - 1 ) { if ( substr( $ARGV[0], $_, 1 ) eq $ARGV[1] ) { $cnt++; } } print $cnt; And if you really want to get cute you can put it all on one line: substr( $ARGV[0], $_, 1 ) eq $ARGV[1] and $cnt++ for 0 .. length( $ARGV[0] ) - 1; print $cnt; :-) John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Variable question
Hi everybody. I am a new user and my first question to this list is probably a very simple one. I am trying to count the number of commas in a variable. The book I am learning from doesn't cover specific information like that. Thanks for any help. Helen - Music, Movies, Sports, Games! Yahoo! Canada Entertainment
Re: Variable question
The tr operator will translate one character to another. For example: my $string = abc; $string =~ tr/a/d/; print $string; prints dbc; However, it also returns the number of changes it did. So, if you don't give it anything to change to, you can count how many occurrences of a character were in the string. my $string = a,b,c; my $num = $string =~ tr/,//; print $num; prints 2. - Original Message - From: Helen Dynah [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, April 05, 2002 12:52 PM Subject: Variable question Hi everybody. I am a new user and my first question to this list is probably a very simple one. I am trying to count the number of commas in a variable. The book I am learning from doesn't cover specific information like that. Thanks for any help. Helen - Music, Movies, Sports, Games! Yahoo! Canada Entertainment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Variable question
Just for the sake of argument, you can also do it using the /g switch of m//. while($string =~ /,/g){ $num++; } -Original Message- From: Tanton Gibbs [mailto:[EMAIL PROTECTED]] Sent: Friday, April 05, 2002 10:06 AM To: Helen Dynah; [EMAIL PROTECTED] Subject: Re: Variable question The tr operator will translate one character to another. For example: my $string = abc; $string =~ tr/a/d/; print $string; prints dbc; However, it also returns the number of changes it did. So, if you don't give it anything to change to, you can count how many occurrences of a character were in the string. my $string = a,b,c; my $num = $string =~ tr/,//; print $num; prints 2. - Original Message - From: Helen Dynah [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, April 05, 2002 12:52 PM Subject: Variable question Hi everybody. I am a new user and my first question to this list is probably a very simple one. I am trying to count the number of commas in a variable. The book I am learning from doesn't cover specific information like that. Thanks for any help. Helen - Music, Movies, Sports, Games! Yahoo! Canada Entertainment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This email may contain confidential and privileged material for the sole use of the intended recipient. If you are not the intended recipient, please contact the sender and delete all copies. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
or, to continue to discussion: @s = $string =~ /,/g; print scalar @s,\n; i don't know how to get count directly assigned to variable. someone? On Friday, April 5, 2002, at 10:29 AM, Timothy Johnson wrote: Just for the sake of argument, you can also do it using the /g switch of m//. while($string =~ /,/g){ $num++; } -Original Message- From: Tanton Gibbs [mailto:[EMAIL PROTECTED]] Sent: Friday, April 05, 2002 10:06 AM To: Helen Dynah; [EMAIL PROTECTED] Subject: Re: Variable question The tr operator will translate one character to another. For example: my $string = abc; $string =~ tr/a/d/; print $string; prints dbc; However, it also returns the number of changes it did. So, if you don't give it anything to change to, you can count how many occurrences of a character were in the string. my $string = a,b,c; my $num = $string =~ tr/,//; print $num; prints 2. - Original Message - From: Helen Dynah [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, April 05, 2002 12:52 PM Subject: Variable question Hi everybody. I am a new user and my first question to this list is probably a very simple one. I am trying to count the number of commas in a variable. The book I am learning from doesn't cover specific information like that. Thanks for any help. Helen - Music, Movies, Sports, Games! Yahoo! Canada Entertainment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This email may contain confidential and privileged material for the sole use of the intended recipient. If you are not the intended recipient, please contact the sender and delete all copies. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Variable question
On Fri, 2002-04-05 at 13:46, bob ackerman wrote: or, to continue to discussion: @s = $string =~ /,/g; print scalar @s,\n; i don't know how to get count directly assigned to variable. someone? snip / I believe it is as simple as: $count = () = $string =~ /,/g; -- Today is Setting Orange the 22nd day of Discord in the YOLD 3168 Fnord. Missile Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Fwd: Variable question
Thanks everyone for all the help. The suggestions worked great. Helen Note: forwarded message attached. Music, Movies, Sports, Games! Yahoo! Canada Entertainment---BeginMessage--- Hi everybody. I am a new user and my first question to this list is probably a very simple one. I am trying to count the number of commas in a variable. The book I am learning from doesn't cover specific information like that. Thanks for any help. Helen - Music, Movies, Sports, Games! Yahoo! Canada Entertainment ---End Message--- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Environment variable question
--- Hitesh Ray [EMAIL PROTECTED] wrote: I am required to modify an Environment variable from one value to another using perl script. I can access the env. variables in the perl script using ENV. How can i modify so that when I exit my perl script -- the env. variable has new value. That's a tricky one. I suppose you're in a shell that supports backticks? Have the script print the new, desired value to STDOUT. Then try something like VAR=`script.pl` This has to be done in the environment where the variable resides. You can't have a process change the environment of its parent. What's the context? I assume it's a command-line operation, but that the script has to figure out what the new value is to be. If that's not the case, what *are* you trying to do? __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
Re: Environment variable question
I am trying to get [one program to pass some info to another] There's many ways to skin that cat! (Apologies to my four cats). I suggest creating a file which contains the directory name.