RE: Grep a variable

2006-05-19 Thread Ryan Frantz


> -Original Message-
> From: Umesh T G [mailto:[EMAIL PROTECTED]
> Sent: Friday, May 19, 2006 8:54 AM
> To: Perl Beginners
> Subject: Grep a variable
> 
> Hi List,
> 
> I am trying to grep a variable from a scalar value. Here is the
example
> below.
> 
> $var = "mydisk";
> $line = "mydisk is bad";
> 
> if (grep/\$var/,$line) {

\$ tells perl to treat the '$' as a literal character.

Try:

$var = "mydisk";
$line = "mydisk is bad";

if ( grep /$var/, $line ) {
print "disk is not bad\n";
}

And _always_:

use strict;
use warnings;

And remember to scope your variables:

http://perl.plover.com/FAQs/Namespaces.html

ry

> print "disk is not bad";
> }
> 
> This does not seems to work.
> 
> Please help me out in figuring out this.
> 
> TIA,
> 
> Cheers,
> Umesh
> 
> --
> 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: Grep a variable

2006-05-19 Thread Ricardo SIGNES
* Umesh T G <[EMAIL PROTECTED]> [2006-05-19T08:54:17]
> I am trying to grep a variable from a scalar value. Here is the example 
> below.
> 
> $var = "mydisk";
> $line = "mydisk is bad";
> if (grep/\$var/,$line) {
>print "disk is not bad";
> }

The simplest answer, already given, is that you should lose the backslash
before the $ in $var, but I think that's not helpful enough.

You're using the "grep EXPR, LIST" form of grep, which evaluates an expression
for each element of the LIST and returns either the number of things for which
it was true, or those things for which it was true.  I suggest never using this
form.  Instead, use the "grep BLOCK LIST" form, which replaces that expression
with a little snippet of code.  This is more flexible and often clearer.

You could rewrite your line as:  if (grep { /$var/ } $line) {

Then again, since $var isn't a regular expression, you could just check whether
the contents of $var occur in $line:

  if (grep { index($_, $var) > -1 } $line) {

That says, "if any of the given elements have the contents of $var in them
somewhere..."  That way you can avoid using the regular expression engine,
which might be slower or do things you didn't expect, depending on the contents
of $var.

Then again, since you're only using one thing, not a list, you could skip grep
entirely!

  if (index($line, $var) > -1) {

-- 
rjbs


signature.asc
Description: Digital signature


Re: Grep a variable

2006-05-19 Thread Jay Savage

On 5/19/06, Umesh T G <[EMAIL PROTECTED]> wrote:

Hi List,

I am trying to grep a variable from a scalar value. Here is the example below.

$var = "mydisk";
$line = "mydisk is bad";

if (grep/\$var/,$line) {
print "disk is not bad";
}

[snip]

TIA,

Cheers,
Umesh



As others have pointed out, get rid of the slash. But to go a little
further here, why are you using grep()? grep() is for lists, and you
don't have a list; $line is a scalar.

   if ($line =~ /$var/) {
   print "disk is not bad";
   }

See perlre or perlretut for details.

HTH,

-- jay
--
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!