Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Problem with ampersand and regex

by frozenwithjoy (Priest)
on Dec 19, 2012 at 20:39 UTC ( #1009631=note: print w/replies, xml ) Need Help??

in reply to Problem with ampersand and regex

Others have already answered your question, but I just wanted to point out something related to the title of your post since it could potentially cause confusion. An ampersand is '&' (the Perl sigil for subroutine), whereas '@' is called at sign, at symbol, apetail, commercial at, or even ampersat.

Replies are listed 'Best First'.
Re^2: Problem with ampersand and regex
by Anonymous Monk on Dec 19, 2012 at 20:46 UTC
    You are correct and thank you for pointing that out. Unfortunately, I am still having problems though. In an attempt to make the post easy to read, I left out some of the code and that makes the answer thus far impossible as far as I can see. Here is how my code should have looked:
    foreach my $line (@array) { if ($line =~ /mail: /) { my $mail = substr($line, $+[$#+] ); print "mail=$mail\n"; } }
    With that, I see no way to use the single quotes as suggested. Any other possible solutions??

    Thank you again.

      What do you get if you capture?

      if( $line =~ /^mail:\s+([\S]+)$/ ) { my $mail = $1; print "mail=$mail\n"; }


        I get ? ?

      The '@' isn't a problem when you read it from a file. Only if you're using it in a double-quoted string. For example:

      $ cat #!/usr/bin/perl use strict; use warnings; my @array = <DATA>; foreach my $line (@array) { if ($line =~ /mail: /) { my $mail = substr($line, $+[$#+] ); print "mail=$mail\n"; } } __DATA__ This @ should be just fine mail: this @ should also be fine $ perl mail=this @ should also be fine


      When your only tool is a hammer, all problems look like your thumb.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009631]
[erix]: I have figured it out. I actually constructed them correctly but used a faulty url *headdesk*
[erix]: ( you use anchors to jump to via a #-suffix to the url -- I am sure you know )
[choroba]: ah, these

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-06-19 12:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.