Tags: assign, assigning, beenable, code, ive, match, pattern, perl, programming, realized, statementif, variable, varible, written

Assigning a match to a varible without an IF

On Programmer » Perl

5,063 words with 4 Comments; publish: Wed, 30 Apr 2008 11:20:00 GMT; (200124.02, « »)

I realized that, out of all of the PERL code Ive written, Ive only been

able to assign to a variable from a pattern match within an IF statement:

if ( /^From:(.*)$/ )

{

$From = $1;

}

Simple enough. But now I wanted to go deeper and capture only the

email address.

Problem is:

$from_ = $From =~ /.perl.todaysummary.com./;

returns 1.

I didnt want the return status. In this example I wanted .perl.todaysummary.com.

And I didnt want to use another IF statement

if ( $From =~ /(.perl.todaysummary.com.)/ )

{

$from_ = $1;

}

Ive searched perl.com and Google but Im not quite sure what to

search for. I tried googling "PERL assign* match*" (w/o the quotes)

So is there a non-IF statement way of doing this?

Thanks

Birl

Please do not CC me responses to my own posts.

I'll read the responses on the list.

All Comments

Leave a comment...

  • 4 Comments
    • From: "S.A. Birl" <sbirl+PERL.perl.todaysummary.com.concept.temple.edu>

      > I realized that, out of all of the PERL code Ive written, Ive only

      > been able to assign to a variable from a pattern match within an IF

      > statement:

      > if ( /^From:(.*)$/ )

      > {

      > $From = $1;

      > }

      > Simple enough. But now I wanted to go deeper and capture only the

      > email address.

      > Problem is:

      > $from_ = $From =~ /.perl.todaysummary.com./;

      > returns 1.

      > I didnt want the return status. In this example I wanted .perl.todaysummary.com.

      > And I didnt want to use another IF statement

      > if ( $From =~ /(.perl.todaysummary.com.)/ )

      > {

      > $from_ = $1;

      > }

      ($from_) = $From =~ /(.perl.todaysummary.com.)/;

      Both pairs of brackets are needed.

      Jenda

      ===== Jenda.perl.todaysummary.com.Krynicky.cz === http://Jenda.Krynicky.cz =====

      When it comes to wine, women and song, wizards are allowed

      to get drunk and croon as much as they like.

      -- Terry Pratchett in Sourcery

      #1; Wed, 30 Apr 2008 11:21:00 GMT
    • S.A. Birl [SAB], on Tuesday, December 14, 2004 at 09:58 (-0500 (EST))

      contributed this to our collective wisdom:

      SAB> Problem is:

      SAB> $from_ = $From =~ /.perl.todaysummary.com./;

      SAB> returns 1.

      you should use this:

      ($from_ = $From) =~ /.perl.todaysummary.com./;

      but your regexp isn't right.

      maybe this will work:

      ($from_ = $From) =~ /\S.perl.todaysummary.com.\S/;

      you should look on google for "right" email regexp, there are some on

      the web...

      ..m8s, cu l8r, Brano.

      ["That's how bold the stuff is, you little priss-ant!" -- Crow]

      #2; Wed, 30 Apr 2008 11:22:00 GMT
    • > I realized that, out of all of the PERL code Ive written, Ive only been

      > able to assign to a variable from a pattern match within an IF statement:

      > if ( /^From:(.*)$/ )

      > {

      > $From = $1;

      > }

      > Simple enough. But now I wanted to go deeper and capture only the

      > email address.

      > Problem is:

      > $from_ = $From =~ /.perl.todaysummary.com./;

      > returns 1.

      if((my $from_= $From) =~ /.perl.todaysummary.com./) {

      print "$from_ (which is now a copy of $From) has an at in it\n";

      }

      if (my ($from_) = $From =~ /^(.*.perl.todaysummary.com..*)$/ ) {

      print "$from_ looks passes a nave test for email address";

      }

      > I didnt want the return status. In this example I wanted .perl.todaysummary.com.

      > And I didnt want to use another IF statement

      > if ( $From =~ /(.perl.todaysummary.com.)/ )

      > {

      > $from_ = $1;

      > }

      >

      There is no way to get around the IF statement (or it's moral

      equivalent) because there are two separate things happening - the

      assignment of the match , and the test to see if such assignment

      occured.

      You could do something like:

      my ($from_) = $From =~ /^From:\s+(.*)$/;

      # here $from_ is either your match or undef

      Which just allows you to defer the test for undef until later.

      print "I saw an email address $from_\n" if defined($from_);

      I just wrote about this a mere two ws ago in this very forum.

      http://www.nntp.perl.org/group/perl.beginners/70671

      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

      Lawrence Statton - lawrenabae.perl.todaysummary.com.abaluon.abaom s/aba/c/g

      Computer software consists of only two components: ones and

      zeros, in roughly equal proportions. All that is required is to

      sort them into the correct order.

      #3; Wed, 30 Apr 2008 11:23:00 GMT
    • On Dec 14, Jenda Krynicky (nospam-Jenda.perl.todaysummary.com.Krynicky.cz.ns) typed:

      Jenda: > I didnt want the return status. In this example I wanted .perl.todaysummary.com.

      Jenda: > And I didnt want to use another IF statement

      Jenda: > if ( $From =~ /(.perl.todaysummary.com.)/ )

      Jenda: > {

      Jenda: > $from_ = $1;

      Jenda: > }

      Jenda:

      Jenda: ($from_) = $From =~ /(.perl.todaysummary.com.)/;

      Jenda:

      Jenda: Both pairs of brackets are needed.

      Jenda:

      Jenda: Jenda

      Excellent! That's what I was looking for.

      I saw examples of those, but the left-hand side had more than one

      variable. So I kept thinking the () were optional.

      Now I know ...

      #4; Wed, 30 Apr 2008 11:25:00 GMT