Re: regex matching statements

2024-06-18 Thread Levi Elias Nystad-Johansen via beginners
Hi, 

Yes, they are the same. 

I like to use $_ only when the data comes in $_ naturally. Like in a for loop: 
for (qw< abc >)
{
  if ( !/\w+\d+/ )
  {
print "not matched";
  }
}

Otherwise, I have to write $_, then I prefer to name the variable something 
descriptive instead. 
Makes the code more readable and maintainable down the road. 


-L 


On Wednesday, June 19th, 2024 at 3:55 AM, Jeff Peng via beginners 
 wrote:

> Hello list,
> 
> are these statements the same in perl?
> 
> $ perl -le '$_="abc";if (!/\w+\d+/){print "not matched"}'
> not matched
> 
> $ perl -le '$_="abc";if ($_ !~ /\w+\d+/){print "not matched"}'
> not matched
> 
> or which is the better one?
> 
> Thanks.
> 
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




regex matching statements

2024-06-18 Thread Jeff Peng via beginners

Hello list,

are these statements the same in perl?

$ perl -le '$_="abc";if (!/\w+\d+/){print "not matched"}'
not matched

$ perl -le '$_="abc";if ($_ !~ /\w+\d+/){print "not matched"}'
not matched

or which is the better one?

Thanks.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: difficulty with matching

2018-06-04 Thread Shlomi Fish
On Sat, 2 Jun 2018 07:30:48 -0500
p...@reason.net wrote:

> Very useful advice, Shlomi; thanks!
> 

you are welcome! Also see http://perl-begin.org/tutorials/bad-elements/ , which
I also maintain.

> 
> > On Jun 2, 2018, at 3:57 AM, Shlomi Fish  wrote:
> > 
> > On Fri, 1 Jun 2018 15:54:55 -0500
> > Rick T mailto:p...@reason.net>> wrote:
> >   
> >> This is a newbie question, I’m sure. But I get perplexed easily!
> >> 
> >> The follow code segment expects to receive a $student_id consisting of a
> >> surname followed by a hyphen followed by a number. The die is for testing
> >> what I’m doing. 
> >> 
> >> If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected. But
> >> if I feed it ‘jones-‘ omitting the number on the end, it dies with no
> >> matches at all (or blanks): ‘ , , ‘ and I cannot figure out why it does
> >> not die with ‘jones, -, ' and would appreciate an explanation. Many thanks!
> >> 
> >>$student_id = lc $student_info_hash{student_id}; # Impose lower case
> >>$student_id =~ s/\s//xmsg;   # Remove whitespace
> >> 
> >>$student_id =~
> >>m{
> >>\A([a-z]+)  # match and capture leading alphabetics 
> >>(-) # hyphen to separate surname from student number
> >>([0-9]+\z)  # match and capture trailing digits
> >> }xms;  # Perl Best Practices
> >>$student_surname = $1;
> >>my $hyphen   = $2;
> >>$student_number  = $3;  
> > 
> > In addition to the other comments, Instead of that, you should do:
> > 
> > if (my ($surname, $hyphen, $num) = $student_id =~ m{ ... }xms)
> > {
> > # match successful.
> > }
> > else
> > {
> > die "match failed ...";
> > }
> > 
> > always check for regex match successes and use the return captures of regex
> > ops. See PBP for more.
> >   
> >> die "$student_surname, $hyphen, $student_number”;
> >> 
> >> Rick Triplett
> >>   
> > 
> > 
> > 
> > -- 
> > -
> > Shlomi Fish   http://www.shlomifish.org/ 
> > Best Introductory Programming Language - http://shlom.in/intro-lang
> > 
> > 
> > A horse! A horse! My kingdom for a horse!
> >— https://en.wikiquote.org/wiki/Richard_III_%28play%29
> > 
> > 
> > Please reply to list if it's a mailing list post - http://shlom.in/reply
> >  .
> > 
> > --
> > To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> >  For additional commands, e-mail:
> > beginners-h...@perl.org 
> > http://learn.perl.org/   



-- 
-
Shlomi Fish   http://www.shlomifish.org/
NSA Factoids - http://www.shlomifish.org/humour/bits/facts/NSA/

Chuck Norris wrote an interpreter for a Turing-complete language using only
NOPs.
— http://www.shlomifish.org/humour/bits/facts/Chuck-Norris/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: difficulty with matching

2018-06-03 Thread Mike Flannigan


Please make an executable code without the hash (just
use a variable) and with the DATA fed below __DATA__
in the script.  Then I would suggest putting an "if"
in front of the match line and print "It Matched.\n".

Maybe put a elsif with only 2 matches.

If you keep playing with it like this you can at least
figure out what it is doing, but maybe not why.  The
manpages will probably be needed to answer that.

Please let us know what you find out.


Mike



On 6/1/2018 3:55 PM, beginners-digest-h...@perl.org wrote:

Subject:
difficulty with matching
From:
Rick T 
Date:
6/1/2018 3:54 PM

To:
Perl Beginners 


This is a newbie question, I’m sure. But I get perplexed easily!

The follow code segment expects to receive a $student_id consisting of 
a surname followed by a hyphen followed by a number. The die is for 
testing what I’m doing.


If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected. 
But if I feed it ‘jones-‘ omitting the number on the end, it dies with 
no matches at all (or blanks): ‘ , , ‘ and I cannot figure out why it 
does not die with ‘jones, -, ' and would appreciate an explanation. 
Many thanks!


    $student_id = lc $student_info_hash{student_id}; # Impose lower case
    $student_id =~ s/\s//xmsg;                       # Remove whitespace

    $student_id =~
        m{
            \A([a-z]+)  # match and capture leading alphabetics
            (-)         # hyphen to separate surname from student number
            ([0-9]+\z)  # match and capture trailing digits
         }xms;          # Perl Best Practices
    $student_surname = $1;
    my $hyphen       = $2;
    $student_number  = $3;
die "$student_surname, $hyphen, $student_number”;

Rick Triplett




Re: difficulty with matching

2018-06-02 Thread perl
Very useful advice, Shlomi; thanks!


> On Jun 2, 2018, at 3:57 AM, Shlomi Fish  wrote:
> 
> On Fri, 1 Jun 2018 15:54:55 -0500
> Rick T mailto:p...@reason.net>> wrote:
> 
>> This is a newbie question, I’m sure. But I get perplexed easily!
>> 
>> The follow code segment expects to receive a $student_id consisting of a
>> surname followed by a hyphen followed by a number. The die is for testing
>> what I’m doing. 
>> 
>> If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected. But if
>> I feed it ‘jones-‘ omitting the number on the end, it dies with no matches at
>> all (or blanks): ‘ , , ‘ and I cannot figure out why it does not die with
>> ‘jones, -, ' and would appreciate an explanation. Many thanks!
>> 
>>$student_id = lc $student_info_hash{student_id}; # Impose lower case
>>$student_id =~ s/\s//xmsg;   # Remove whitespace
>> 
>>$student_id =~
>>m{
>>\A([a-z]+)  # match and capture leading alphabetics 
>>(-) # hyphen to separate surname from student number
>>([0-9]+\z)  # match and capture trailing digits
>> }xms;  # Perl Best Practices
>>$student_surname = $1;
>>my $hyphen   = $2;
>>$student_number  = $3;
> 
> In addition to the other comments, Instead of that, you should do:
> 
> if (my ($surname, $hyphen, $num) = $student_id =~ m{ ... }xms)
> {
>   # match successful.
> }
> else
> {
>   die "match failed ...";
> }
> 
> always check for regex match successes and use the return captures of regex
> ops. See PBP for more.
> 
>> die "$student_surname, $hyphen, $student_number”;
>> 
>> Rick Triplett
>> 
> 
> 
> 
> -- 
> -
> Shlomi Fish   http://www.shlomifish.org/ 
> Best Introductory Programming Language - http://shlom.in/intro-lang 
> 
> 
> A horse! A horse! My kingdom for a horse!
>— https://en.wikiquote.org/wiki/Richard_III_%28play%29 
> 
> 
> Please reply to list if it's a mailing list post - http://shlom.in/reply 
>  .
> 
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org 
> 
> For additional commands, e-mail: beginners-h...@perl.org 
> 
> http://learn.perl.org/ 


Re: difficulty with matching

2018-06-02 Thread Shlomi Fish
On Fri, 1 Jun 2018 15:54:55 -0500
Rick T  wrote:

> This is a newbie question, I’m sure. But I get perplexed easily!
> 
> The follow code segment expects to receive a $student_id consisting of a
> surname followed by a hyphen followed by a number. The die is for testing
> what I’m doing. 
> 
> If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected. But if
> I feed it ‘jones-‘ omitting the number on the end, it dies with no matches at
> all (or blanks): ‘ , , ‘ and I cannot figure out why it does not die with
> ‘jones, -, ' and would appreciate an explanation. Many thanks!
> 
> $student_id = lc $student_info_hash{student_id}; # Impose lower case
> $student_id =~ s/\s//xmsg;   # Remove whitespace
> 
> $student_id =~
> m{
> \A([a-z]+)  # match and capture leading alphabetics 
> (-) # hyphen to separate surname from student number
> ([0-9]+\z)  # match and capture trailing digits
>  }xms;  # Perl Best Practices
> $student_surname = $1;
> my $hyphen   = $2;
> $student_number  = $3;

In addition to the other comments, Instead of that, you should do:

if (my ($surname, $hyphen, $num) = $student_id =~ m{ ... }xms)
{
# match successful.
}
else
{
die "match failed ...";
}

always check for regex match successes and use the return captures of regex
ops. See PBP for more.

> die "$student_surname, $hyphen, $student_number”;
> 
> Rick Triplett
> 



-- 
-
Shlomi Fish   http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang

A horse! A horse! My kingdom for a horse!
— https://en.wikiquote.org/wiki/Richard_III_%28play%29

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: difficulty with matching

2018-06-01 Thread Andy Bach
 > If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected.
But if I feed it ‘jones-‘ omitting the number on the end, it dies with no
matches at all (or blanks): ‘ , , ‘ and I cannot figure out why it does not
die with ‘jones, -, ' and would appreciate an explanation. Many thanks!

$student_id = lc $student_info_hash{student_id}; # Impose lower case
$student_id =~ s/\s//xmsg;   # Remove whitespace

$student_id =~
m{
\A([a-z]+)  # match and capture leading alphabetics
(-) # hyphen to separate surname from student number
([0-9]+\z)  # match and capture trailing digits
 }xms;  # Perl Best Practices
$student_surname = $1;
my $hyphen   = $2;
$student_number  = $3;
die "$student_surname, $hyphen, $student_number”;

([0-9]+\z)

means "capture one or more digits (same as "\d+") at the end of the
string/before a newline - so
m{
\A([a-z]+)  # match and capture leading alphabetics
(-) # hyphen to separate surname from student number
([0-9]*)\z  # match and capture trailing digits
 }xms;  # Perl Best Practices

zero or more digits

On Fri, Jun 1, 2018 at 3:54 PM, Rick T  wrote:

> This is a newbie question, I’m sure. But I get perplexed easily!
>
> The follow code segment expects to receive a $student_id consisting of a
> surname followed by a hyphen followed by a number. The die is for testing
> what I’m doing.
>
> If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected. But
> if I feed it ‘jones-‘ omitting the number on the end, it dies with no
> matches at all (or blanks): ‘ , , ‘ and I cannot figure out why it does not
> die with ‘jones, -, ' and would appreciate an explanation. Many thanks!
>
> $student_id = lc $student_info_hash{student_id}; # Impose lower case
> $student_id =~ s/\s//xmsg;   # Remove whitespace
>
> $student_id =~
> m{
> \A([a-z]+)  # match and capture leading alphabetics
> (-) # hyphen to separate surname from student number
> ([0-9]+\z)  # match and capture trailing digits
>  }xms;  # Perl Best Practices
> $student_surname = $1;
> my $hyphen   = $2;
> $student_number  = $3;
> die "$student_surname, $hyphen, $student_number”;
>
> Rick Triplett
>
>


-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Re: difficulty with matching

2018-06-01 Thread Octavian Rasnita
[0-9]+ means at least one digit in range 0-9.
If there is no digit, the string won't match.
You want [0-9]* instead.

--Octavian

  - Original Message - 
  From: Rick T 
  To: Perl Beginners 
  Sent: Friday, June 01, 2018 11:54 PM
  Subject: difficulty with matching


  This is a newbie question, I’m sure. But I get perplexed easily!


  The follow code segment expects to receive a $student_id consisting of a 
surname followed by a hyphen followed by a number. The die is for testing what 
I’m doing. 


  If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected. But if 
I feed it ‘jones-‘ omitting the number on the end, it dies with no matches at 
all (or blanks): ‘ , , ‘ and I cannot figure out why it does not die with 
‘jones, -, ' and would appreciate an explanation. Many thanks!


  $student_id = lc $student_info_hash{student_id}; # Impose lower case
  $student_id =~ s/\s//xmsg;   # Remove whitespace

  $student_id =~
  m{
  \A([a-z]+)  # match and capture leading alphabetics 
  (-) # hyphen to separate surname from student number
  ([0-9]+\z)  # match and capture trailing digits
   }xms;  # Perl Best Practices
  $student_surname = $1;
  my $hyphen   = $2;
  $student_number  = $3;
  die "$student_surname, $hyphen, $student_number”;


  Rick Triplett



difficulty with matching

2018-06-01 Thread Rick T
This is a newbie question, I’m sure. But I get perplexed easily!

The follow code segment expects to receive a $student_id consisting of a 
surname followed by a hyphen followed by a number. The die is for testing what 
I’m doing. 

If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected. But if I 
feed it ‘jones-‘ omitting the number on the end, it dies with no matches at all 
(or blanks): ‘ , , ‘ and I cannot figure out why it does not die with ‘jones, 
-, ' and would appreciate an explanation. Many thanks!

$student_id = lc $student_info_hash{student_id}; # Impose lower case
$student_id =~ s/\s//xmsg;   # Remove whitespace

$student_id =~
m{
\A([a-z]+)  # match and capture leading alphabetics 
(-) # hyphen to separate surname from student number
([0-9]+\z)  # match and capture trailing digits
 }xms;  # Perl Best Practices
$student_surname = $1;
my $hyphen   = $2;
$student_number  = $3;
die "$student_surname, $hyphen, $student_number”;

Rick Triplett



Re: Regex for matching files that don't have type extensions

2016-11-05 Thread Shawn H Corey
On Sat, 05 Nov 2016 21:30:12 +
Aaron Wells  wrote:

> True. It could get hairy. Unicode is a pretty vast landscape, and I
> think if you only want ASCII word characters to count as things that
> could be in a filename, your original [A-Za-z0-9_] is your best bet.
> Thanks to the others for their comments. As Ken says: there are
> probably more ways to code this.

TIMTOWTDI
https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it

;)

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regex for matching files that don't have type extensions

2016-11-05 Thread Octavian Rasnita
From: Aaron Wells 


  True. It could get hairy. Unicode is a pretty vast landscape, and I think if 
you only want ASCII word characters to count as things that could be in a 
filename, your original [A-Za-z0-9_] is your best bet. Thanks to the others for 
their comments. As Ken says: there are probably more ways to code this.




Another (shorter) way of writing that can be:

/^\w+$/aa

Where /aa makes \w mean just [A-Za-z0-9_].

a = ASCII and aa is used for double protection, so only ASCII is used.

--Octavian



Re: Regex for matching files that don't have type extensions

2016-11-05 Thread X Dungeness
On Sat, Nov 5, 2016 at 10:55 AM, Jovan Trujillo
 wrote:
> Hi Aaron,
>In perlre I read that \w
> "
>
> \w[3]  Match a "word" character (alphanumeric plus "_", plus
>   other connector punctuation chars plus
> Unicode
>   marks)
>
> "
>
> So since I didn't know what these 'other' connection punctuation chars are I
> avoided it. Unicode makes things more complicated for me. Do you know?
>

To exclude Unicode and ensure only ASCII, use the /a modifer,
eg,  /\w+/a

>From perlre:

/a

   is the same as "/u", except that "\d", "\s", "\w", and the Posix
   character classes are restricted to matching in the ASCII range only.
   That is, with this modifier, "\d" always means precisely the digits "0"
   to "9"; "\s" means the five characters "[ \f\n\r\t]"; "\w" means the 63
   characters "[A-Za-z0-9_]"; and likewise, all the Posix classes such as
   "[[:print:]]" match only the appropriate ASCII-range characters.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regex for matching files that don't have type extensions

2016-11-05 Thread Aaron Wells
True. It could get hairy. Unicode is a pretty vast landscape, and I think
if you only want ASCII word characters to count as things that could be in
a filename, your original [A-Za-z0-9_] is your best bet. Thanks to the
others for their comments. As Ken says: there are probably more ways to
code this.

On Sat, Nov 5, 2016, 11:44 AM Kent Fredric  wrote:

> On 6 November 2016 at 06:14, Jovan Trujillo 
> wrote:
> >
> > 1207003PE_GM_09TNPLM2.csv
> >
> > I originally though m/[A-Za-z0-9\_]+/ would work, but it captures both
> > strings.
> > So then I tried m/[A-Za-z0-9\_]+(?!\.)/ but I still get both strings
> > captured.
>
> Alternatively, if your use case allows it, it might be more viable to
> use negative matching.
>
>   $file !~ /[.]/ and print "$file has no extension"
>
> There's probably a reason why you're not doing this already, but can't
> tell from the context.
>
> NB: Clearly defining what an "extension" means is also pertinent:
>
> fooo.csv
> fooo.jpg
> fooo.jpeg
> foo.tar.xz
> foo.config
> .config
> .config.ini
>
> You probably are just meaning "has a dot" or "has a dot followed by at
> most 3 characters", but its hard to tell from context ( and there are
> a lot of obvious cases where there is an "extension" suffix that is
> greater than 3 characters )
>
>
>
>
>
>
>
> --
> Kent
>
> KENTNL - https://metacpan.org/author/KENTNL
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Regex for matching files that don't have type extensions

2016-11-05 Thread Kent Fredric
On 6 November 2016 at 06:14, Jovan Trujillo  wrote:
>
> 1207003PE_GM_09TNPLM2.csv
>
> I originally though m/[A-Za-z0-9\_]+/ would work, but it captures both
> strings.
> So then I tried m/[A-Za-z0-9\_]+(?!\.)/ but I still get both strings
> captured.

Alternatively, if your use case allows it, it might be more viable to
use negative matching.

  $file !~ /[.]/ and print "$file has no extension"

There's probably a reason why you're not doing this already, but can't
tell from the context.

NB: Clearly defining what an "extension" means is also pertinent:

fooo.csv
fooo.jpg
fooo.jpeg
foo.tar.xz
foo.config
.config
.config.ini

You probably are just meaning "has a dot" or "has a dot followed by at
most 3 characters", but its hard to tell from context ( and there are
a lot of obvious cases where there is an "extension" suffix that is
greater than 3 characters )







-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regex for matching files that don't have type extensions

2016-11-05 Thread Ken Slater
Hi Jovan,

On Sat, Nov 5, 2016 at 1:14 PM, Jovan Trujillo 
wrote:

> Hi All,
> I thought I could use a simple regex to match files like this:
>
> 1207003PE_GM_09TNPLM2
>
> and ignore files with extensions like this:
>
> 1207003PE_GM_09TNPLM2.csv
>
> I originally though m/[A-Za-z0-9\_]+/ would work, but it captures both
> strings.
> So then I tried m/[A-Za-z0-9\_]+(?!\.)/ but I still get both strings
> captured.
>
> What am I doing wrong?
>
> Thank you,
> Jovan
>

The regular expression *m/[A-Za-z0-9\_]+(?!\.)/* will match, as it will
match one or more of the desired characters (*1207003PE_GM_09TNPLM*) that
are followed by a character (*2*) that is not a period/dot.

There are probably many ways to code this. The simplest may be to run two
regular expressions - the first to determine if there is a period/dot (*.*)
in the string.

HTH, Ken


Re: Regex for matching files that don't have type extensions

2016-11-05 Thread Jovan Trujillo
Hi Aaron,
   In perlre I read that \w
"

- \w[3]  Match a "word" character (alphanumeric plus "_", plus
-   other connector punctuation
chars plus Unicode
-   marks)

"

So since I didn't know what these 'other' connection punctuation chars are
I avoided it. Unicode makes things more complicated for me. Do you know?


Thanks,

Jovan

On Sat, Nov 5, 2016 at 10:27 AM, Aaron Wells  wrote:

> *predefined
>
> On Sat, Nov 5, 2016, 10:27 AM Aaron Wells  wrote:
>
>> Hi Jovan. \w is a presidents character classes that is equivalent to
>> [A-Za-z0-9_], so this works also:
>> m/^\w+$/
>>
>> On Sat, Nov 5, 2016, 10:24 AM Jovan Trujillo 
>> wrote:
>>
>> Ah, I figured it out.
>>  m/^[A-Za-z0-9_]+$/ works because it will only match if the entire string
>> follows the pattern. Thanks!
>>
>> On Sat, Nov 5, 2016 at 10:14 AM, Jovan Trujillo <
>> jovan.trujil...@gmail.com> wrote:
>>
>> Hi All,
>> I thought I could use a simple regex to match files like this:
>>
>> 1207003PE_GM_09TNPLM2
>>
>> and ignore files with extensions like this:
>>
>> 1207003PE_GM_09TNPLM2.csv
>>
>> I originally though m/[A-Za-z0-9\_]+/ would work, but it captures both
>> strings.
>> So then I tried m/[A-Za-z0-9\_]+(?!\.)/ but I still get both strings
>> captured.
>>
>> What am I doing wrong?
>>
>> Thank you,
>> Jovan
>>
>>
>>


Re: Regex for matching files that don't have type extensions

2016-11-05 Thread Aaron Wells
*predefined

On Sat, Nov 5, 2016, 10:27 AM Aaron Wells  wrote:

> Hi Jovan. \w is a presidents character classes that is equivalent to
> [A-Za-z0-9_], so this works also:
> m/^\w+$/
>
> On Sat, Nov 5, 2016, 10:24 AM Jovan Trujillo 
> wrote:
>
> Ah, I figured it out.
>  m/^[A-Za-z0-9_]+$/ works because it will only match if the entire string
> follows the pattern. Thanks!
>
> On Sat, Nov 5, 2016 at 10:14 AM, Jovan Trujillo  > wrote:
>
> Hi All,
> I thought I could use a simple regex to match files like this:
>
> 1207003PE_GM_09TNPLM2
>
> and ignore files with extensions like this:
>
> 1207003PE_GM_09TNPLM2.csv
>
> I originally though m/[A-Za-z0-9\_]+/ would work, but it captures both
> strings.
> So then I tried m/[A-Za-z0-9\_]+(?!\.)/ but I still get both strings
> captured.
>
> What am I doing wrong?
>
> Thank you,
> Jovan
>
>
>


Re: Regex for matching files that don't have type extensions

2016-11-05 Thread Aaron Wells
Hi Jovan. \w is a presidents character classes that is equivalent to
[A-Za-z0-9_], so this works also:
m/^\w+$/

On Sat, Nov 5, 2016, 10:24 AM Jovan Trujillo 
wrote:

> Ah, I figured it out.
>  m/^[A-Za-z0-9_]+$/ works because it will only match if the entire string
> follows the pattern. Thanks!
>
> On Sat, Nov 5, 2016 at 10:14 AM, Jovan Trujillo  > wrote:
>
> Hi All,
> I thought I could use a simple regex to match files like this:
>
> 1207003PE_GM_09TNPLM2
>
> and ignore files with extensions like this:
>
> 1207003PE_GM_09TNPLM2.csv
>
> I originally though m/[A-Za-z0-9\_]+/ would work, but it captures both
> strings.
> So then I tried m/[A-Za-z0-9\_]+(?!\.)/ but I still get both strings
> captured.
>
> What am I doing wrong?
>
> Thank you,
> Jovan
>
>
>


Re: Regex for matching files that don't have type extensions

2016-11-05 Thread Jovan Trujillo
Ah, I figured it out.
 m/^[A-Za-z0-9_]+$/ works because it will only match if the entire string
follows the pattern. Thanks!

On Sat, Nov 5, 2016 at 10:14 AM, Jovan Trujillo 
wrote:

> Hi All,
> I thought I could use a simple regex to match files like this:
>
> 1207003PE_GM_09TNPLM2
>
> and ignore files with extensions like this:
>
> 1207003PE_GM_09TNPLM2.csv
>
> I originally though m/[A-Za-z0-9\_]+/ would work, but it captures both
> strings.
> So then I tried m/[A-Za-z0-9\_]+(?!\.)/ but I still get both strings
> captured.
>
> What am I doing wrong?
>
> Thank you,
> Jovan
>


Regex for matching files that don't have type extensions

2016-11-05 Thread Jovan Trujillo
Hi All,
I thought I could use a simple regex to match files like this:

1207003PE_GM_09TNPLM2

and ignore files with extensions like this:

1207003PE_GM_09TNPLM2.csv

I originally though m/[A-Za-z0-9\_]+/ would work, but it captures both
strings.
So then I tried m/[A-Za-z0-9\_]+(?!\.)/ but I still get both strings
captured.

What am I doing wrong?

Thank you,
Jovan


Re: Increasing a counter while matching against list of given words.

2014-05-13 Thread Mike Dunaway

That makes sense, thanks.

On 05/13/2014 01:46 PM, Shlomi Fish wrote:

Hi Mike,

please reply to the list.

On Tue, 13 May 2014 12:49:42 -0500
Mike Dunaway  wrote:


Actually, can you tell me what's going on here:

my %words_lookup = (map { $_ => 1 } @words_to_look_for);


I build a hash called %words_lookup whose keys are the entries of
@words_to_look_for and all of its values are 1. See:

http://perldoc.perl.org/functions/map.html

That way I can tell if a word is in @words_to_look_for quickly by doing:
exists($words_lookup{$word}).

Regards,

Shlomi Fish



That would be most helpful.

On 05/13/2014 03:08 AM, Shlomi Fish wrote:

my %words_lookup = (map { $_ => 1 } @words_to_look_for);






--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Increasing a counter while matching against list of given words.

2014-05-13 Thread Shlomi Fish
Hi Mike,

please reply to the list.

On Tue, 13 May 2014 12:49:42 -0500
Mike Dunaway  wrote:

> Actually, can you tell me what's going on here:
> 
> my %words_lookup = (map { $_ => 1 } @words_to_look_for);
> 

I build a hash called %words_lookup whose keys are the entries of
@words_to_look_for and all of its values are 1. See:

http://perldoc.perl.org/functions/map.html

That way I can tell if a word is in @words_to_look_for quickly by doing:
exists($words_lookup{$word}).

Regards,

Shlomi Fish


> That would be most helpful.
> 
> On 05/13/2014 03:08 AM, Shlomi Fish wrote:
> > my %words_lookup = (map { $_ => 1 } @words_to_look_for);
> 



-- 
-
Shlomi Fish   http://www.shlomifish.org/
Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/

  Khisanth =~ s/must sleep/must give Botje all my money/ .
— Freenode’s #perl

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Increasing a counter while matching against list of given words.

2014-05-13 Thread Mike Dunaway

Thanks, I'll look into this and give it a try.

On 05/13/2014 03:08 AM, Shlomi Fish wrote:

Hi Mike,

On Tue, 13 May 2014 02:36:30 -0500
Mike Dunaway  wrote:


Hello everyone. Let's say I have a user provide @list of words and I
want to match each words against a file or a another @list of words and
increase a counter every time a word in the given list appears in what
I'm matching against, what might a possible solution look like for that?
The only thing I could think of is a nasty mess of a ton of for loops
restarted until you get to the end of the user provided list which may
end up being very slow depending on the size of the list. What if I
wanted to also use regular expressions?


It sounds like what you want is a hash:

http://perl-begin.org/topics/hashes/ (NOTE: perl-begin.org is my domain).

Untested code:

< CODE >

use strict;
use warnings;

my @words_to_look_for = ( ... );

my %words_lookup = (map { $_ => 1 } @words_to_look_for);

my $counter = 0;

foreach my $word (@list_of_words_to_search)
{
if (exists $words_lookup{$word})
{
$counter++;
}
}

< / CODE >

Regards,

Shlomi Fish




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Increasing a counter while matching against list of given words.

2014-05-13 Thread Shlomi Fish
Hi Mike,

On Tue, 13 May 2014 02:36:30 -0500
Mike Dunaway  wrote:

> Hello everyone. Let's say I have a user provide @list of words and I 
> want to match each words against a file or a another @list of words and 
> increase a counter every time a word in the given list appears in what 
> I'm matching against, what might a possible solution look like for that? 
> The only thing I could think of is a nasty mess of a ton of for loops 
> restarted until you get to the end of the user provided list which may 
> end up being very slow depending on the size of the list. What if I 
> wanted to also use regular expressions?
> 

It sounds like what you want is a hash:

http://perl-begin.org/topics/hashes/ (NOTE: perl-begin.org is my domain).

Untested code:

< CODE >

use strict;
use warnings;

my @words_to_look_for = ( ... );

my %words_lookup = (map { $_ => 1 } @words_to_look_for);

my $counter = 0;

foreach my $word (@list_of_words_to_search)
{
if (exists $words_lookup{$word})
{
$counter++;
}
}

< / CODE >

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Why I Love Perl - http://shlom.in/joy-of-perl

“Publish or Perish” → “Life or Death”
— http://unarmed.shlomifish.org/2615.html

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Increasing a counter while matching against list of given words.

2014-05-13 Thread Mike Dunaway
Hello everyone. Let's say I have a user provide @list of words and I 
want to match each words against a file or a another @list of words and 
increase a counter every time a word in the given list appears in what 
I'm matching against, what might a possible solution look like for that? 
The only thing I could think of is a nasty mess of a ton of for loops 
restarted until you get to the end of the user provided list which may 
end up being very slow depending on the size of the list. What if I 
wanted to also use regular expressions?


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: match not matching

2014-03-01 Thread Jim Gibson

On Feb 28, 2014, at 9:13 PM, Bill McCormick  wrote:

> Can somebody help me understand this? Given this loop, and the logged output 
> following ...
> 
> my $found;
> for( @$products ) {;
>  $found = $$_ =~ m|$project|;
>  $dump = Data::Dumper->Dump([$_, $project, $$_, $found]);
>  $logger->trace(qq(dump=$dump));
> }
> 
> I can't explain why $found is not true on the 3rd pass. Does this have 
> something to do with the way I'm dereferencing the blessed object?
> 
> SVN.Hooks - dump=$VAR1 = bless( do{\(my $o = 'FS1100-S1')}, 
> 'RPC::XML::string' );
> $VAR2 = 'STABLE/FS1100-S3/RSLOGIX_5000/';
> $VAR3 = 'FS1100-S1';
> $VAR4 = '';
> SVN.Hooks - dump=$VAR1 = bless( do{\(my $o = 'FS1100-S2')}, 
> 'RPC::XML::string' );
> $VAR2 = 'STABLE/FS1100-S3/RSLOGIX_5000/';
> $VAR3 = 'FS1100-S2';
> $VAR4 = '';
> SVN.Hooks - dump=$VAR1 = bless( do{\(my $o = 'FS1100-S3')}, 
> 'RPC::XML::string' );
> $VAR2 = 'STABLE/FS1100-S3/RSLOGIX_5000/';
> $VAR3 = 'FS1100-S3';
> $VAR4 = '';


You have inverted the regular expression. You want this:

for( @$products ) {;
 $found = $project =~ m|$$_|;


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: match not matching

2014-03-01 Thread Dr.Ruud

On 2014-03-01 06:13, Bill McCormick wrote:


   $found = $$_ =~ m|$project|;


Alternative:

$found = ( index( $project, $$_ ) >= 0 );

or rather use:

$found = $project =~ /\b\Q$$_\E\b/;

Always add escaping and anchors, or you will match the unmatchable.

--
Ruud



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: match not matching

2014-03-01 Thread Bill McCormick

On 3/1/2014 6:19 AM, Paul Johnson wrote:

On Fri, Feb 28, 2014 at 11:13:05PM -0600, Bill McCormick wrote:

Can somebody help me understand this? Given this loop, and the
logged output following ...

my $found;
for( @$products ) {;
   $found = $$_ =~ m|$project|;


I think you might have meant:
 $found = $project =~ m|$$_|;


Duh!! Thanks!


---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: match not matching

2014-03-01 Thread Paul Johnson
On Fri, Feb 28, 2014 at 11:13:05PM -0600, Bill McCormick wrote:
> Can somebody help me understand this? Given this loop, and the
> logged output following ...
> 
> my $found;
> for( @$products ) {;
>   $found = $$_ =~ m|$project|;

I think you might have meant:
$found = $project =~ m|$$_|;

>   $dump = Data::Dumper->Dump([$_, $project, $$_, $found]);
>   $logger->trace(qq(dump=$dump));
> }
> 
> I can't explain why $found is not true on the 3rd pass. Does this
> have something to do with the way I'm dereferencing the blessed
> object?

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




match not matching

2014-02-28 Thread Bill McCormick
Can somebody help me understand this? Given this loop, and the logged 
output following ...


my $found;
for( @$products ) {;
  $found = $$_ =~ m|$project|;
  $dump = Data::Dumper->Dump([$_, $project, $$_, $found]);
  $logger->trace(qq(dump=$dump));
}

I can't explain why $found is not true on the 3rd pass. Does this have 
something to do with the way I'm dereferencing the blessed object?


SVN.Hooks - dump=$VAR1 = bless( do{\(my $o = 'FS1100-S1')}, 
'RPC::XML::string' );

$VAR2 = 'STABLE/FS1100-S3/RSLOGIX_5000/';
$VAR3 = 'FS1100-S1';
$VAR4 = '';
SVN.Hooks - dump=$VAR1 = bless( do{\(my $o = 'FS1100-S2')}, 
'RPC::XML::string' );

$VAR2 = 'STABLE/FS1100-S3/RSLOGIX_5000/';
$VAR3 = 'FS1100-S2';
$VAR4 = '';
SVN.Hooks - dump=$VAR1 = bless( do{\(my $o = 'FS1100-S3')}, 
'RPC::XML::string' );

$VAR2 = 'STABLE/FS1100-S3/RSLOGIX_5000/';
$VAR3 = 'FS1100-S3';
$VAR4 = '';

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: matching certain lines

2013-02-10 Thread Chris Stinemetz
On Sun, Feb 10, 2013 at 8:05 AM, Chris Stinemetz
wrote:

> To take this a step further.
>
> How would you go about creating a hash to sum up all the values in group
> $3 utilizing the flip/flop operator and print the results of the key and
> value with the key being group $2?
>
>
This is what I came up with and seems to do the trick. Hopefully it can
help someone else.

my %hash;
while () {
if ( /0x3\|68\|/ .. /^#END/ ) {
if (/\|(\d)\|(\d+)\|(\d+)/) {
my @cols = ( $1, $2, $3 );
$hash{ $cols[1] }{instance}++;
$hash{ $cols[1] }{volume} += $cols[2];
}
}
}


## header
print join(",", qw( Peg Instance Subtotal )), "\n";

foreach ( sort { $a <=> $b } keys(%hash) ) {
print join( ",", $_, $hash{$_}{instance}, $hash{$_}{volume} ), "\n";
}


Re: matching certain lines

2013-02-10 Thread Chris Stinemetz
To take this a step further.

How would you go about creating a hash to sum up all the values in group $3
utilizing the flip/flop operator and print the results of the key and value
with the key being group $2?

Thank you,

Chris

while(  ) {
if ( /0x3\|68\|/ .. /^#END/ ) {
print if /\|68\|/;
print join(",", $1, $2, $3 ), "\n" if /\|(\d)\|(\d+)\|(\d+)/;
}
}

__DATA__
#LOGNUM|110|OPERATIONAL
|NETWORK.1:SUBNETWORK.100:EBSC.1:EBSCSHELF.4:DSFPVCARD.5
|6261.2.0.330.1.1|6261.2.0.330.1.1|0x3|68|1|1
|ACP2|12
|7|1|0
|7|2|2636
|7|3|0
|7|4|0
|7|5|2601
|7|6|0
|7|7|0
|7|8|0
|7|9|0
|7|10|0
|7|11|0
|7|12|0
#END


#LOGNUM|134|OPERATIONAL
|NETWORK.1:SUBNETWORK.100:EBSC.1:EBSCSHELF.4:DSFPVCARD.5
|6261.2.0.330.1.1|6261.2.0.330.1.1|0x3|68|1|1
|ACP5|12
|7|1|0
|7|2|2638
|7|3|0
|7|4|0
|7|5|2592
|7|6|0
|7|7|0
|7|8|0
|7|9|0
|7|10|0
|7|11|0
|7|12|0
#END


#LOGNUM|150|OPERATIONAL
|NETWORK.1:SUBNETWORK.100:EBSC.1:EBSCSHELF.4:DSFPVCARD.7
|6263.2.0.330.1.1|6263.2.0.330.1.1|0x3|68|1|1
|ACP1|12
|7|1|0
|7|2|2573
|7|3|0
|7|4|0
|7|5|2551
|7|6|0
|7|7|0
|7|8|0
|7|9|0
|7|10|0
|7|11|0
|7|12|0
#END


Re: matching certain lines

2013-02-10 Thread Chris Stinemetz
Thank you everyone for you help.

-Chris


On Sat, Feb 9, 2013 at 5:15 PM, Uri Guttman  wrote:

> On 02/09/2013 03:59 PM, John W. Krahn wrote:
>
>
>> Let's re-factor that down to its essence:
>>
>> while (  ) {
>>  print if /\|68\|/;
>>  print "$1\t$2\t$3\n" if /(\|\d)\|(\d+)\|(\d+)/;
>>  }
>>
>> Now we need to add something that starts at |68| and stops at #END:
>>
>> while (  ) {
>>  if ( /\|68\|/ .. /^#END/ ) {
>>  print if /\|68\|/;
>>  print "$1\t$2\t$3\n" if /(\|\d)\|(\d+)\|(\d+)/;
>>  }
>>  }
>>
>
> there is a great feature with the .. flip/flop op that isn't well known.
> it returns not just a boolean state but a count of where it is in the
> range. so you can use that value to handle the first line differently and
> not need to copy the regex which can lead to a bug if it changes and you
> forget to edit both copies:
>
> if ( my $range = /\|68\|/ .. /^#END/ ) {
> print if $range == 1 ;
>
> the last line in the range gets a number with E0 appended so it is the
> same value but you can check for the /E/ and do something there:
>
> print "DONE\n" if $range =~ /E/ ;
>
> uri
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: matching certain lines

2013-02-09 Thread Uri Guttman

On 02/09/2013 03:59 PM, John W. Krahn wrote:



Let's re-factor that down to its essence:

while (  ) {
 print if /\|68\|/;
 print "$1\t$2\t$3\n" if /(\|\d)\|(\d+)\|(\d+)/;
 }

Now we need to add something that starts at |68| and stops at #END:

while (  ) {
 if ( /\|68\|/ .. /^#END/ ) {
 print if /\|68\|/;
 print "$1\t$2\t$3\n" if /(\|\d)\|(\d+)\|(\d+)/;
 }
 }


there is a great feature with the .. flip/flop op that isn't well known. 
it returns not just a boolean state but a count of where it is in the 
range. so you can use that value to handle the first line differently 
and not need to copy the regex which can lead to a bug if it changes and 
you forget to edit both copies:


if ( my $range = /\|68\|/ .. /^#END/ ) {
print if $range == 1 ;

the last line in the range gets a number with E0 appended so it is the 
same value but you can check for the /E/ and do something there:


print "DONE\n" if $range =~ /E/ ;

uri



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: matching certain lines

2013-02-09 Thread timothy adigun
Hi,
On Sat, Feb 9, 2013 at 6:06 PM, Chris Stinemetz wrote:

> I would like to only work with the data that has a line with |68| in it
>

  Does it mean even, that |68| can be anyway in the your data before any of
the #END is reached?
If yes, then I think you have been given a good solution. If not, you might
want to see how to make *the* |68|  you want different from any other ones
that might occur in your data.

Like you  have this:
#LOGNUM|68|OPERATIONAL 

...
#END

then somewhere else you have this:
|5883.2.0.330.1.1|5883.2.0.330.1.1|0x3|68|1|1


#END


print that line and then print each subsequent lines in that match
> /\|7\|\d+\|\d+/ until #END is reached and then repeat for the rest of the
> input data.
>
> Below is what I have attempted.
>
> Thanks in advance.
>
> Chris
>
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my ( $col1, $col2, $col3 );
>
> while( my $line =  ) {
> chomp($line);
> if ( $line =~ /(.*\|68\|.*)/ ) {
> my $OM = $1;
> print $OM, "\n";
> }
>  if ( $line =~ /(\|\d)\|(\d+)\|(\d+)/ ) {
> $col1 = $1;
> $col2 = $2;
> $col3 = $3;
> print join("\t", $col1, $col2, $col3 ), "\n";
> }
> }
>
>
> __DATA__
> #LOGNUM|68|OPERATIONAL
> |NETWORK.1:SUBNETWORK.100:EBSC.1:EBSCSHELF.3:DSFPVCARD.6
> |5883.2.0.330.1.1|5883.2.0.330.1.1|0x3|19|1|1
> |ACP3|8
> |7|1|9
> |7|2|436
> |7|3|5
> |7|4|0
> |7|5|0
> |7|6|0
> |7|7|0
> |7|8|0
> #END
>
>
> #LOGNUM|69|OPERATIONAL
> |NETWORK.1:SUBNETWORK.100:EBSC.1:EBSCSHELF.3:DSFPVCARD.6
> |5883.2.0.330.1.1|5883.2.0.330.1.1|0x3|26|1|1
> |ACP3|19
> |7|1|0
> |7|2|0
> |7|3|0
> |7|4|0
> |7|5|0
> |7|6|0
> |7|7|0
> |7|8|0
> |7|9|0
> |7|10|20
> |7|11|0
> |7|12|0
> |7|13|0
> |7|14|0
> |7|15|0
> |7|16|0
> |7|17|0
> |7|18|0
> |7|19|0
> #END
>
>
> #LOGNUM|70|OPERATIONAL
> |NETWORK.1:SUBNETWORK.100:EBSC.1:EBSCSHELF.3:DSFPVCARD.6
> |5883.2.0.330.1.1|5883.2.0.330.1.1|0x3|68|1|1
> |ACP3|12
> |7|1|0
> |7|2|2610
> |7|3|0
> |7|4|0
> |7|5|2575
> |7|6|0
> |7|7|0
> |7|8|0
> |7|9|0
> |7|10|0
> |7|11|0
> |7|12|0
> #END
>



-- 
Tim


Re: matching certain lines

2013-02-09 Thread John W. Krahn

Chris Stinemetz wrote:


I would like to only work with the data that has a line with |68| in it
print that line and then print each subsequent lines in that match
/\|7\|\d+\|\d+/ until #END is reached and then repeat for the rest of the
input data.

Below is what I have attempted.

Thanks in advance.

Chris


#!/usr/bin/perl

use warnings;
use strict;

my ( $col1, $col2, $col3 );

while( my $line =  ) {
chomp($line);
if ( $line =~ /(.*\|68\|.*)/ ) {
my $OM = $1;
print $OM, "\n";
}
  if ( $line =~ /(\|\d)\|(\d+)\|(\d+)/ ) {
$col1 = $1;
$col2 = $2;
$col3 = $3;
print join("\t", $col1, $col2, $col3 ), "\n";
}
}


Let's re-factor that down to its essence:

while (  ) {
print if /\|68\|/;
print "$1\t$2\t$3\n" if /(\|\d)\|(\d+)\|(\d+)/;
}

Now we need to add something that starts at |68| and stops at #END:

while (  ) {
if ( /\|68\|/ .. /^#END/ ) {
print if /\|68\|/;
print "$1\t$2\t$3\n" if /(\|\d)\|(\d+)\|(\d+)/;
}
}



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: matching certain lines

2013-02-09 Thread David Precious
On Sat, 9 Feb 2013 11:06:46 -0600
Chris Stinemetz  wrote:

> I would like to only work with the data that has a line with |68| in
> it print that line and then print each subsequent lines in that match
> /\|7\|\d+\|\d+/ until #END is reached and then repeat for the rest of
> the input data.

OTTOMH,

perl -lne '/\|68\|/ .. /\#END/ && /\|7\|\d+\|\d+/ && print'

For an explanation, look up ".." in perldoc perlop - the flip-flop
operator - it evaluates to true once the first condition (in this case,
the current line matches the regex /\|68\|/ becomes true), and
continues to evaluate to a true value until the second condition is true
(in this case, the current line contains "#END"), at which point it goes
back to false again.  Combining that with a check for the line
containing what you want gets you most of the way there; I think it'll
skip the start & end lines though, so you'll probably want to modify
the last regex to include them.



-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




matching certain lines

2013-02-09 Thread Chris Stinemetz
I would like to only work with the data that has a line with |68| in it
print that line and then print each subsequent lines in that match
/\|7\|\d+\|\d+/ until #END is reached and then repeat for the rest of the
input data.

Below is what I have attempted.

Thanks in advance.

Chris


#!/usr/bin/perl

use warnings;
use strict;

my ( $col1, $col2, $col3 );

while( my $line =  ) {
chomp($line);
if ( $line =~ /(.*\|68\|.*)/ ) {
my $OM = $1;
print $OM, "\n";
}
 if ( $line =~ /(\|\d)\|(\d+)\|(\d+)/ ) {
$col1 = $1;
$col2 = $2;
$col3 = $3;
print join("\t", $col1, $col2, $col3 ), "\n";
}
}


__DATA__
#LOGNUM|68|OPERATIONAL
|NETWORK.1:SUBNETWORK.100:EBSC.1:EBSCSHELF.3:DSFPVCARD.6
|5883.2.0.330.1.1|5883.2.0.330.1.1|0x3|19|1|1
|ACP3|8
|7|1|9
|7|2|436
|7|3|5
|7|4|0
|7|5|0
|7|6|0
|7|7|0
|7|8|0
#END


#LOGNUM|69|OPERATIONAL
|NETWORK.1:SUBNETWORK.100:EBSC.1:EBSCSHELF.3:DSFPVCARD.6
|5883.2.0.330.1.1|5883.2.0.330.1.1|0x3|26|1|1
|ACP3|19
|7|1|0
|7|2|0
|7|3|0
|7|4|0
|7|5|0
|7|6|0
|7|7|0
|7|8|0
|7|9|0
|7|10|20
|7|11|0
|7|12|0
|7|13|0
|7|14|0
|7|15|0
|7|16|0
|7|17|0
|7|18|0
|7|19|0
#END


#LOGNUM|70|OPERATIONAL
|NETWORK.1:SUBNETWORK.100:EBSC.1:EBSCSHELF.3:DSFPVCARD.6
|5883.2.0.330.1.1|5883.2.0.330.1.1|0x3|68|1|1
|ACP3|12
|7|1|0
|7|2|2610
|7|3|0
|7|4|0
|7|5|2575
|7|6|0
|7|7|0
|7|8|0
|7|9|0
|7|10|0
|7|11|0
|7|12|0
#END


Re: Pattern matching to hash

2012-12-29 Thread Dr.Ruud

On 2012-12-28 21:32, twle...@reagan.com wrote:

I hope this is a simple fix.  I want to check the beginning characters of items in a 
hash, and compare that to a scalar variable.  I do not need for the entire value to 
match; just the first couple of characters.  Here is a simple example of what I want, but 
it does not work.  Both "if" statements produce a match.  I have read several 
web pages, but I have not found one that has exactly what I need.  Thank you for any help 
on this.
Tim


my $prefix_search_list = '03S,04S';
my @prefix_array = split /\,/,$prefix_search_list;
my %prefix_hash = map {$_ => 1 } @prefix_array;

#compare 05S to 03S and 04S
my $input_field = "05S885858"; #should not match
if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in 
hash\n"; }
else { print "$input_field is not found\n"; }

#compare 03S to 03S and 04S
$input_field = "03S84844"; #should match
if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in 
hash\n"; }
else { print "$input_field is not found\n"; }


#!/usr/bin/perl -wl
use strict;

my @prefixes = split /,/, '03S,04S';

my @inputs = qw( 05S885858  03S84844 );

for my $input ( @inputs ) {
  for my $prefix ( @prefixes ) {
next if index( $input, $prefix );
print $input, " starts with ", $prefix;
  }
}
__END__

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Pattern matching to hash

2012-12-28 Thread Chris Charley



"timothy adigun"  wrote in message 
news:CAEWzkh6mZohVJn__LRL60AGoqbHkmTPyn=JM=cewcmmftpj...@mail.gmail.com...



Hello Chris,
Please see my comment below.


On Fri, Dec 28, 2012 at 10:24 PM, Chris Charley 
wrote:


[snip]


I only answered the question using a for loop. Am not the one who
originator of the question please.


I wasn't replying to you, I was replying to the OP.  :-)

Chris 



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Pattern matching to hash

2012-12-28 Thread Tim Lewis
Thank you Chris.  Using strict and warnings should have been the first thing 
that I did.  Thank you also for the code correction.

-Original Message-
From: Chris Charley [mailto:char...@pulsenet.com] 
Sent: Friday, December 28, 2012 5:52 PM
To: beginners@perl.org
Subject: Re: Pattern matching to hash



Chris Charley""  wrote in message news
>Tim wrote in message news:1356726727.215915...@webmail.reagan.com...

>>I hope this is a simple fix.  I want to check the beginning characters 
>>of items in a hash, and compare that to a scalar variable.
>>I do not need for the entire value to match; just the first couple of 
>>characters.
>>Tim

[snip]


>Hello Tim,

[snip]



>I might have approached it differently.


>#!/usr/bin/perl
>use strict;
>use warnings;

>my $prefix_search_list = '03S|04S';

>while () {
>print if /^$prefix_search_list/;
>}

>__DATA__
>05S885858
>03S84844
>foo
>bar
>04Sbaz

A correction to my answer:

print if /^$prefix_search_list/; 

should be:

print if /^(?:$prefix_search_list)/;

Chris

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, 
e-mail: beginners-h...@perl.org http://learn.perl.org/




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Pattern matching to hash

2012-12-28 Thread Tim Lewis
Thank you Tim.  The lack of strict and warnings should have been the first
thing that I put in the code.  Thank you for the correction and for the help
in the loop.

 

From: timothy adigun [mailto:2teezp...@gmail.com] 
Sent: Friday, December 28, 2012 4:11 PM
To: twle...@reagan.com
Cc: beginners@perl.org
Subject: Re: Pattern matching to hash

 

Hi,

 


my $prefix_search_list = '03S,04S';
my @prefix_array = split /\,/,$prefix_search_list;
my %prefix_hash = map {$_ => 1 } @prefix_array;

#compare 05S to 03S and 04S
my $input_field = "05S885858"; #should not match

 

  1. using stricts and warnings pragma, shows clearly that there is nothing
like the scalar variable "$prefix_hash". You only have a hash variable with
the name "%prefix_hash". Am sure you will appreciate why one would ask that
such pragma been turned ON. 

 

   2. Using, a "for or foreach" loop, one could compare each of the hash key
with the $input_field. like so:

 

#compare 05S to 03S and 04S

my $input_field = "05S885858";#should not match

foreach my $prefix_match ( keys %prefix_hash ) {

if ( $input_field =~ /$prefix_match/ ) {

print "$input_field is found in hash\n";

}

else { print "$input_field is not found\n"; }

}

 

#compare 03S to 03S and 04S

foreach my $prefix_match ( keys %prefix_hash ) {

$input_field = "03S84844";#should match

if ( $input_field =~ /$prefix_match/ ) {

print "$input_field is found in hash\n";

}

else { print "$input_field is not found\n"; }

}

 

Below is the full script:

 

use warnings;

use strict;

 

my $prefix_search_list = '03S,04S';

my @prefix_array   = split /\,/, $prefix_search_list;

my %prefix_hash= map { $_ => 1 } @prefix_array;

 

#compare 05S to 03S and 04S

my $input_field = "05S885858";#should not match

foreach my $prefix_match ( keys %prefix_hash ) {

if ( $input_field =~ /$prefix_match/ ) {

print "$input_field is found in hash\n";

}

else { print "$input_field is not found\n"; }

}

 

#compare 03S to 03S and 04S

foreach my $prefix_match ( keys %prefix_hash ) {

$input_field = "03S84844";#should match

if ( $input_field =~ /$prefix_match/ ) {

print "$input_field is found in hash\n";

}

else { print "$input_field is not found\n"; }

}

 


Hope this helps.


 

-- 
Tim



Re: Pattern matching to hash

2012-12-28 Thread Chris Charley



Chris Charley""  wrote in message news

Tim wrote in message news:1356726727.215915...@webmail.reagan.com...



I hope this is a simple fix.  I want to check the beginning characters of
items in a hash, and compare that to a scalar variable.
I do not need for the entire value to match; just the first couple of 
characters.

Tim


[snip]



Hello Tim,


[snip]




I might have approached it differently.




#!/usr/bin/perl
use strict;
use warnings;



my $prefix_search_list = '03S|04S';



while () {
   print if /^$prefix_search_list/;
}



__DATA__
05S885858
03S84844
foo
bar
04Sbaz


A correction to my answer:

print if /^$prefix_search_list/; 


should be:

print if /^(?:$prefix_search_list)/;

Chris

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Pattern matching to hash

2012-12-28 Thread timothy adigun
 Hello Chris,
Please see my comment below.

On Fri, Dec 28, 2012 at 10:24 PM, Chris Charley wrote:

>
>
> Tim wrote in message news:1356726727.215915216@**webmail.reagan.com...
>
>  I hope this is a simple fix.  I want to check the beginning characters of
>> items in a hash, and compare that to a scalar variable.
>> I do not need for the entire value to match; just the first couple of
>> characters.
>> Tim
>>
>>
>> my $prefix_search_list = '03S,04S';
>> my @prefix_array = split /\,/,$prefix_search_list;
>> my %prefix_hash = map {$_ => 1 } @prefix_array;
>>
>> #compare 05S to 03S and 04S
>> my $input_field = "05S885858"; #should not match
>> if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in
>> hash\n"; }
>> else { print "$input_field is not found\n"; }
>>
>> #compare 03S to 03S and 04S
>> $input_field = "03S84844"; #should match
>> if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in
>> hash\n"; }
>> else { print "$input_field is not found\n"; }
>>
>
> Hello Tim,
>
>  I only answered the question using a for loop. Am not the one who
originator of the question please.


> If you had used strict it would have told you of the error. You should
> always
> start your scripts with:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
>
> There is no variable declared as: $prefix_hash. That is just a
> (undeclared) scalar
> variable. You wanted the hash you created instead, I believe.
>
> The above was also my submission too. Please, check my reply to the OP.


> To use your hash, something like the following will produce the results
> you want.
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $prefix_search_list = '03S,04S';
> my %prefix_hash = map {$_ => 1} split /\,/, $prefix_search_list;
>
>
> #compare 05S to 03S and 04S
> my $input_field = "05S885858"; #should not match
> if ( $prefix_hash{ substr $input_field, 0, 3 } ) { print "$input_field is
> found in hash\n"; }
>
> else { print "$input_field is not found\n"; }
>
> #compare 03S to 03S and 04S
> $input_field = "03S84844"; #should match
> if ( $prefix_hash{ substr $input_field, 0, 3 } ) { print "$input_field is
> found in hash\n"; }
>
> else { print "$input_field is not found\n"; }
>
>
>
>
> I might have approached it differently.
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $prefix_search_list = '03S|04S';
>
> while () {
>print if /^$prefix_search_list/;
> }
>
> __DATA__
> 05S885858
> 03S84844
> foo
> bar
> 04Sbaz
>
>
> *** prints
>
> C:\Old_Data\perlp>perl t1.pl
> 03S84844
> 04Sbaz
>
>
>
> Hope this helps,
> Chris
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
Tim


Re: Pattern matching to hash

2012-12-28 Thread Chris Charley



Tim wrote in message news:1356726727.215915...@webmail.reagan.com...


I hope this is a simple fix.  I want to check the beginning characters of
items in a hash, and compare that to a scalar variable.
I do not need for the entire value to match; just the first couple of 
characters.

Tim


my $prefix_search_list = '03S,04S';
my @prefix_array = split /\,/,$prefix_search_list;
my %prefix_hash = map {$_ => 1 } @prefix_array;

#compare 05S to 03S and 04S
my $input_field = "05S885858"; #should not match
if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in 
hash\n"; }

else { print "$input_field is not found\n"; }

#compare 03S to 03S and 04S
$input_field = "03S84844"; #should match
if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in 
hash\n"; }

else { print "$input_field is not found\n"; }


Hello Tim,

If you had used strict it would have told you of the error. You should 
always

start your scripts with:

#!/usr/bin/perl
use strict;
use warnings;


There is no variable declared as: $prefix_hash. That is just a (undeclared) 
scalar

variable. You wanted the hash you created instead, I believe.

To use your hash, something like the following will produce the results
you want.


#!/usr/bin/perl
use strict;
use warnings;

my $prefix_search_list = '03S,04S';
my %prefix_hash = map {$_ => 1} split /\,/, $prefix_search_list;

#compare 05S to 03S and 04S
my $input_field = "05S885858"; #should not match
if ( $prefix_hash{ substr $input_field, 0, 3 } ) { print "$input_field is 
found in hash\n"; }

else { print "$input_field is not found\n"; }

#compare 03S to 03S and 04S
$input_field = "03S84844"; #should match
if ( $prefix_hash{ substr $input_field, 0, 3 } ) { print "$input_field is 
found in hash\n"; }

else { print "$input_field is not found\n"; }




I might have approached it differently.


#!/usr/bin/perl
use strict;
use warnings;

my $prefix_search_list = '03S|04S';

while () {
   print if /^$prefix_search_list/;
}

__DATA__
05S885858
03S84844
foo
bar
04Sbaz


*** prints

C:\Old_Data\perlp>perl t1.pl
03S84844
04Sbaz



Hope this helps,
Chris 



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Pattern matching to hash

2012-12-28 Thread timothy adigun
Hi,


> my $prefix_search_list = '03S,04S';
> my @prefix_array = split /\,/,$prefix_search_list;
> my %prefix_hash = map {$_ => 1 } @prefix_array;
>
> #compare 05S to 03S and 04S
> my $input_field = "05S885858"; #should not match
>

  1. using stricts and warnings pragma, shows clearly that there is nothing
like the scalar variable "$prefix_hash". You only have a hash variable with
the name "%prefix_hash". Am sure you will appreciate why one would ask that
such pragma been turned ON.

   2. Using, a "for or foreach" loop, one could compare each of the hash
key with the $input_field. like so:

#compare 05S to 03S and 04S
my $input_field = "05S885858";#should not match
foreach my $prefix_match ( keys %prefix_hash ) {
if ( $input_field =~ /$prefix_match/ ) {
print "$input_field is found in hash\n";
}
else { print "$input_field is not found\n"; }
}

#compare 03S to 03S and 04S
foreach my $prefix_match ( keys %prefix_hash ) {
$input_field = "03S84844";#should match
if ( $input_field =~ /$prefix_match/ ) {
print "$input_field is found in hash\n";
}
else { print "$input_field is not found\n"; }
}

Below is the full script:

use warnings;
use strict;

my $prefix_search_list = '03S,04S';
my @prefix_array   = split /\,/, $prefix_search_list;
my %prefix_hash= map { $_ => 1 } @prefix_array;

#compare 05S to 03S and 04S
my $input_field = "05S885858";#should not match
foreach my $prefix_match ( keys %prefix_hash ) {
if ( $input_field =~ /$prefix_match/ ) {
print "$input_field is found in hash\n";
}
else { print "$input_field is not found\n"; }
}

#compare 03S to 03S and 04S
foreach my $prefix_match ( keys %prefix_hash ) {
$input_field = "03S84844";#should match
if ( $input_field =~ /$prefix_match/ ) {
print "$input_field is found in hash\n";
}
else { print "$input_field is not found\n"; }
}


Hope this helps.

-- 
Tim


Pattern matching to hash

2012-12-28 Thread twlewis
I hope this is a simple fix.  I want to check the beginning characters of items 
in a hash, and compare that to a scalar variable.  I do not need for the entire 
value to match; just the first couple of characters.  Here is a simple example 
of what I want, but it does not work.  Both "if" statements produce a match.  I 
have read several web pages, but I have not found one that has exactly what I 
need.  Thank you for any help on this.
Tim


my $prefix_search_list = '03S,04S';
my @prefix_array = split /\,/,$prefix_search_list;
my %prefix_hash = map {$_ => 1 } @prefix_array;

#compare 05S to 03S and 04S
my $input_field = "05S885858"; #should not match
if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in 
hash\n"; }
else { print "$input_field is not found\n"; }

#compare 03S to 03S and 04S
$input_field = "03S84844"; #should match
if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in 
hash\n"; }
else { print "$input_field is not found\n"; }


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Multiple matching of a group of characters

2012-10-03 Thread Brandon McCaig
On Tue, Oct 02, 2012 at 11:19:51PM +0100, Florian Huber wrote:
> The string is:
> 
> >ENSG0112365|ENST0230122|109783797|109787053TGTTTCACAATTCACTACTAAATGTGTACCATTAAATTGAACAGAAAGCTGAGGAATGAACTTCAAGCATTACAAG
> 
> So I'm trying to retrieve'ENSG0112365', 'ENST0230122' and
> the sequence bit, starting with a 'T' and get rid of the junk in
> between.
> 
> code:
> 
> /#!/usr/bin/perl//
> //
> //use strict;//
> //use warnings;//
> //
> //my $gene;//
> //my @elements = <>;//
> //
> //foreach $gene (@elements) {//
> //$gene =~ />(ENSG\d*) \| (ENST\d*) .*? ([AGCT]*)/x;//
> //print "$1 $2 $3\n";//
> //}/
> 
> 
> This will print "ENSG0112365 ENST0230122"
> 
> without the sequence.

You might want to simplify the problem first. It looks like the
first two fields that you're interested in are delimited with the
vertical bar (|) character. You can split the string first on
that to get those 4 parts in between. Then you can just match the
simple character class that you want from the last sequence.

#!/usr/bin/perl

use strict;
use warnings;

my $sequence = 
'>ENSG0112365|ENST0230122|109783797|109787053TGTTTCACAATTCACTACTAAATGTGTACCATTAAATTGAACAGAAAGCTGAGGAATGAACTTCAAGCATTACAAG';

my ($foo, $bar, $baz) = parse_sequence($sequence);

print "foo=$foo\n";
print "bar=$bar\n";
print "baz=$baz\n";

sub parse_sequence {
my ($sequence) = @_;

# We don't want the leading > character.
$sequence =~ s/^>//;

my @parts = split /\|/, $sequence;

# We're interested in the first two fields.
my ($foo, $bar) = @parts;

# And the matching part of the last one.
my ($baz) = $parts[3] =~ /([ACGT]+)/;

# You may prefer to return a hash reference or something with
# meaningful field names. Those details are entirely up to
# you.
return ($foo, $bar, $baz);
}

__END__

Output:

foo=ENSG0112365
bar=ENST0230122
baz=TGTTTCACAATTCACTACTAAATGTGTACCATTAAATTGAACAGAAAGCTGAGGAATGAACTTCAAGCATTACAAG

Obviously you should assign appropriate names to the variables.
:)

> Originally I had .* before the ([ACGT]) so I figured it's
> greedy and will eat the sequence away. ? makes it nongreedy,
> doesn't it? Still doesn't work.
> 
> Other results:
> 
> with ([AGCT])* it says that $3 is uninitialised - so here it
> didn't match at all???
> 
> with ([AGCT]{5}) it works fine - it returns TGTTT.
> 
> 
> This I found kinda strange - looks like I've got something with
> the greediness/precedence wrong?

It takes some practice (and often a clear head) to get regular
expressions right. :) The * will only match if the rest of the
regular expression allows it to. If not it is happy to not match
anything. If you require at least one match then be sure you use
+. :) And as above, always try to make things as simple as
possible. It's much easier to get simple correct. :)

Regards,


-- 
Brandon McCaig  
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bamccaig.com/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'



signature.asc
Description: Digital signature


Re: Multiple matching of a group of characters

2012-10-03 Thread Florian Huber
Alright, thanks for your answers! I think I know what my mistake was: although 
I realised that * means 0 or more I thought that it would prefer to match as 
often as possible - but it seems that the matching stops as soon as the regex 
is successful, that's why.

Thanks again.

Flo

 Original-Nachricht 
> Datum: Tue, 2 Oct 2012 19:17:58 -0400
> Von: William Muriithi 
> An: Florian Huber 
> CC: beginners@perl.org
> Betreff: Re: Multiple matching of a group of characters

> Florian,
> >
> > The string is:
> >
> >>ENSG0112365|ENST0230122|109783797|109787053TGTTTCACAATTCACTACTAAATGTGTACCATTAAATTGAACAGAAAGCTGAGGAATGAACTTCAAGCATTACAAG
> 
> It may actually have helped if you posted two or three samples.  This
> could help us identify patterns in your data and hence advice on the
> necessary regular expression for process your data
> >
> > So I'm trying to retrieve'ENSG0112365', 'ENST0230122' and the
> > sequence bit, starting with a 'T' and get rid of the junk in between.
> 
> There is a lot of T is the above gene sequence, not sure which one you
> refers to when you say "starting with 'T'"
> 
> >
> > code:
> >
> > /#!/usr/bin/perl//
> > //
> > //use strict;//
> > //use warnings;//
> > //
> > //my $gene;//
> > //my @elements = <>;//
> > //
> > //foreach $gene (@elements) {//
> > //$gene =~ />(ENSG\d*) \| (ENST\d*) .*? ([AGCT]*)/x;//
> Try
> 
> $gene =~ />(ENSG\d*) \| (ENST\d*) .*? (AGCT\z)/x;//
> 
> Assume you need everything starting from AGCT to the end of the sequence
> > //print "$1 $2 $3\n";//
> > //}/
> >
> >
> > This will print "ENSG0112365 ENST0230122"
> >
> > without the sequence. Originally I had .* before the ([ACGT]) so I
> figured
> > it's greedy and will eat the sequence away. ? makes it nongreedy,
> doesn't
> > it? Still doesn't work.
> >
> Greed here don't mean eating, its how wide it try matching.  Try
> google as there is better explanation out there
> > Other results:
> >
> > with ([AGCT])* it says that $3 is uninitialised - so here it didn't
> match at
> > all???
> >
> > with ([AGCT]{5}) it works fine - it returns TGTTT.
> >
> >
> > This I found kinda strange - looks like I've got something with the
> > greediness/precedence wrong?
> >
> >
> > Thank you for your help!
> >
> > Flo
> >
> >
> > On 02/10/2012 01:36, Brandon McCaig wrote:
> >>
> >> On Mon, Oct 01, 2012 at 11:15:53PM +0100, Florian Huber wrote:
> >>>
> >>> Dear all,
> >>
> >> Hello,
> >>
> >>> $string = "/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/"
> >>
> >> I would suggest that you show us the real data. I'm assuming that
> >> 'NOTNEEDED' is a placeholder for some data that you're not
> >> interested in. Without knowing what that is we can't really say
> >> for sure what is going on (though we can speculate; see below).
> >>
> >> Note that you should be using the strict and warnings pragmas
> >> (see below). The lack of 'my' here suggests that you probably
> >> aren't.
> >>
> >>> But when I do
> >>>
> >>> $string =~ /[ACGT]/;
> >>>
> >>> it matches only the last letter, i.e. "G". Why doesn't it start
> >>> at the beginning?
> >>
> >> It isn't matching the last letter. You are probably making the
> >> wrong assumption. This is common when you're having trouble with
> >> code. Again, show us the 'NOTNEEDED' part. :)
> >>
> >>> But it gets even better, I figured that adding the greedy *
> >>> should help:
> >>>
> >>> $string =~ /[ACGT]*/;
> >>>
> >>> and now it doesn't match anything. Shouldn't it try to match as
> >>> many times as possible?
> >>
> >> It should match at least the once that you saw earlier (assuming
> >> the same data).
> >>
> >>> My confusion was complete when I tried
> >>>
> >>> $string =~ /[ACGT]{5}/;
> >>>
> >>> now it matches 5 letters, but this time from the beginning,
> >>> i.e.: ACGAC.
> >>
> >> I'm guessing that the first 'NOTNEEDED' contains a 'G'. That
&

Re: Multiple matching of a group of characters

2012-10-02 Thread William Muriithi
Florian,
>
> The string is:
>
>>ENSG0112365|ENST0230122|109783797|109787053TGTTTCACAATTCACTACTAAATGTGTACCATTAAATTGAACAGAAAGCTGAGGAATGAACTTCAAGCATTACAAG

It may actually have helped if you posted two or three samples.  This
could help us identify patterns in your data and hence advice on the
necessary regular expression for process your data
>
> So I'm trying to retrieve'ENSG0112365', 'ENST0230122' and the
> sequence bit, starting with a 'T' and get rid of the junk in between.

There is a lot of T is the above gene sequence, not sure which one you
refers to when you say "starting with 'T'"

>
> code:
>
> /#!/usr/bin/perl//
> //
> //use strict;//
> //use warnings;//
> //
> //my $gene;//
> //my @elements = <>;//
> //
> //foreach $gene (@elements) {//
> //$gene =~ />(ENSG\d*) \| (ENST\d*) .*? ([AGCT]*)/x;//
Try

$gene =~ />(ENSG\d*) \| (ENST\d*) .*? (AGCT\z)/x;//

Assume you need everything starting from AGCT to the end of the sequence
> //print "$1 $2 $3\n";//
> //}/
>
>
> This will print "ENSG0112365 ENST0230122"
>
> without the sequence. Originally I had .* before the ([ACGT]) so I figured
> it's greedy and will eat the sequence away. ? makes it nongreedy, doesn't
> it? Still doesn't work.
>
Greed here don't mean eating, its how wide it try matching.  Try
google as there is better explanation out there
> Other results:
>
> with ([AGCT])* it says that $3 is uninitialised - so here it didn't match at
> all???
>
> with ([AGCT]{5}) it works fine - it returns TGTTT.
>
>
> This I found kinda strange - looks like I've got something with the
> greediness/precedence wrong?
>
>
> Thank you for your help!
>
> Flo
>
>
> On 02/10/2012 01:36, Brandon McCaig wrote:
>>
>> On Mon, Oct 01, 2012 at 11:15:53PM +0100, Florian Huber wrote:
>>>
>>> Dear all,
>>
>> Hello,
>>
>>> $string = "/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/"
>>
>> I would suggest that you show us the real data. I'm assuming that
>> 'NOTNEEDED' is a placeholder for some data that you're not
>> interested in. Without knowing what that is we can't really say
>> for sure what is going on (though we can speculate; see below).
>>
>> Note that you should be using the strict and warnings pragmas
>> (see below). The lack of 'my' here suggests that you probably
>> aren't.
>>
>>> But when I do
>>>
>>> $string =~ /[ACGT]/;
>>>
>>> it matches only the last letter, i.e. "G". Why doesn't it start
>>> at the beginning?
>>
>> It isn't matching the last letter. You are probably making the
>> wrong assumption. This is common when you're having trouble with
>> code. Again, show us the 'NOTNEEDED' part. :)
>>
>>> But it gets even better, I figured that adding the greedy *
>>> should help:
>>>
>>> $string =~ /[ACGT]*/;
>>>
>>> and now it doesn't match anything. Shouldn't it try to match as
>>> many times as possible?
>>
>> It should match at least the once that you saw earlier (assuming
>> the same data).
>>
>>> My confusion was complete when I tried
>>>
>>> $string =~ /[ACGT]{5}/;
>>>
>>> now it matches 5 letters, but this time from the beginning,
>>> i.e.: ACGAC.
>>
>> I'm guessing that the first 'NOTNEEDED' contains a 'G'. That
>> would explain the first match. The second result is nonesense
>> with the data we've seen. :-/ If 'NOTNEEDED' doesn't contain a
>> string at least 5 characters in length composed only of 'A', 'C',
>> 'G', or 'T' then that would explain this last result.
>>
>>> I fail to understand that behaviour. I checked the Perl
>>> documentation a bit and I sort of understand why /[ACGT]/ only
>>> matches one letter only (but not why it starts at the end).
>>> However, I'm simply puzzled at the other things.
>>
>> As said, provide us with a full (minimal) program to demonstrate
>> the problems you're having if your problems persist.
>>
>> Assuming 'NOTNEEDED' cannot contain '/' characters then you may
>> need to include those in your pattern to make sure you match the
>> parts you want. You will probably want to use captures for that
>> (see perldoc perlre). To understand 

Re: Multiple matching of a group of characters

2012-10-02 Thread Jim Gibson

On Oct 2, 2012, at 3:19 PM, Florian Huber wrote:

> Thanks guys, for the answers. :-)
> 
> I'm sorry I posted a shortened version of the code as I thought it'd make it 
> easier to read while still getting the message across. So here's the actual 
> example and the corresponding output:
> 
> The string is:
> 
> >ENSG0112365|ENST0230122|109783797|109787053TGTTTCACAATTCACTACTAAATGTGTACCATTAAATTGAACAGAAAGCTGAGGAATGAACTTCAAGCATTACAAG
> 
> So I'm trying to retrieve'ENSG0112365', 'ENST0230122' and the 
> sequence bit, starting with a 'T' and get rid of the junk in between.
> 
> code:
> 
> /#!/usr/bin/perl//
> //
> //use strict;//
> //use warnings;//
> //
> //my $gene;//
> //my @elements = <>;//
> //
> //foreach $gene (@elements) {//
> //$gene =~ />(ENSG\d*) \| (ENST\d*) .*? ([AGCT]*)/x;//
> //print "$1 $2 $3\n";//
> //}/

Why all the / characters? Did you put those there, or is it some artifact of 
your email client or mine?

In the future, try posting a complete program that people can run without 
having to generate a data file. In this case, just assign a scalar variable 
with your data line and modify your program to parse that:

my $element = 
q(ENSG0112365|ENST0230122|109783797|109787053TGTTTCACAA...CTTCAAGCATTACAAG);

etc.

> This will print "ENSG0112365 ENST0230122"
> 
> without the sequence. Originally I had .* before the ([ACGT]) so I figured 
> it's greedy and will eat the sequence away. ? makes it nongreedy, doesn't it? 
> Still doesn't work.

You are not realizing that [AGCT]* means "zero or more characters from the set 
A, G, C, and T". You are getting a zero-character match because that is what 
you are asking for. Try ([AGCT]+) that insists on at least one matching 
character and will match the longest successive set of AGTC characters.

> 
> Other results:
> 
> with ([AGCT])* it says that $3 is uninitialised - so here it didn't match at 
> all???
> 

You are telling it "zero or more", so no match is fine, and that will be the 
first thing the RE engine tries, so that is what you get.


> with ([AGCT]{5}) it works fine - it returns TGTTT.
> 
> 
> This I found kinda strange - looks like I've got something with the 
> greediness/precedence wrong?

What you have wrong is telling the RE engine that you don't care about matching 
any characters!


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Multiple matching of a group of characters

2012-10-02 Thread Florian Huber

Thanks guys, for the answers. :-)

I'm sorry I posted a shortened version of the code as I thought it'd 
make it easier to read while still getting the message across. So here's 
the actual example and the corresponding output:


The string is:

>ENSG0112365|ENST0230122|109783797|109787053TGTTTCACAATTCACTACTAAATGTGTACCATTAAATTGAACAGAAAGCTGAGGAATGAACTTCAAGCATTACAAG

So I'm trying to retrieve'ENSG0112365', 'ENST0230122' and the 
sequence bit, starting with a 'T' and get rid of the junk in between.


code:

/#!/usr/bin/perl//
//
//use strict;//
//use warnings;//
//
//my $gene;//
//my @elements = <>;//
//
//foreach $gene (@elements) {//
//$gene =~ />(ENSG\d*) \| (ENST\d*) .*? ([AGCT]*)/x;//
//print "$1 $2 $3\n";//
//}/


This will print "ENSG0112365 ENST0230122"

without the sequence. Originally I had .* before the ([ACGT]) so I 
figured it's greedy and will eat the sequence away. ? makes it 
nongreedy, doesn't it? Still doesn't work.


Other results:

with ([AGCT])* it says that $3 is uninitialised - so here it didn't 
match at all???


with ([AGCT]{5}) it works fine - it returns TGTTT.


This I found kinda strange - looks like I've got something with the 
greediness/precedence wrong?


Thank you for your help!

Flo


On 02/10/2012 01:36, Brandon McCaig wrote:

On Mon, Oct 01, 2012 at 11:15:53PM +0100, Florian Huber wrote:

Dear all,

Hello,


$string = "/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/"

I would suggest that you show us the real data. I'm assuming that
'NOTNEEDED' is a placeholder for some data that you're not
interested in. Without knowing what that is we can't really say
for sure what is going on (though we can speculate; see below).

Note that you should be using the strict and warnings pragmas
(see below). The lack of 'my' here suggests that you probably
aren't.


But when I do

$string =~ /[ACGT]/;

it matches only the last letter, i.e. "G". Why doesn't it start
at the beginning?

It isn't matching the last letter. You are probably making the
wrong assumption. This is common when you're having trouble with
code. Again, show us the 'NOTNEEDED' part. :)


But it gets even better, I figured that adding the greedy *
should help:

$string =~ /[ACGT]*/;

and now it doesn't match anything. Shouldn't it try to match as
many times as possible?

It should match at least the once that you saw earlier (assuming
the same data).


My confusion was complete when I tried

$string =~ /[ACGT]{5}/;

now it matches 5 letters, but this time from the beginning,
i.e.: ACGAC.

I'm guessing that the first 'NOTNEEDED' contains a 'G'. That
would explain the first match. The second result is nonesense
with the data we've seen. :-/ If 'NOTNEEDED' doesn't contain a
string at least 5 characters in length composed only of 'A', 'C',
'G', or 'T' then that would explain this last result.


I fail to understand that behaviour. I checked the Perl
documentation a bit and I sort of understand why /[ACGT]/ only
matches one letter only (but not why it starts at the end).
However, I'm simply puzzled at the other things.

As said, provide us with a full (minimal) program to demonstrate
the problems you're having if your problems persist.

Assuming 'NOTNEEDED' cannot contain '/' characters then you may
need to include those in your pattern to make sure you match the
parts you want. You will probably want to use captures for that
(see perldoc perlre). To understand the below program you will
also need to understand the /x modifier (again see perldoc
perlre).

#!/usr/bin/perl

use strict;   # <---Make sure you have these.
use warnings; # <--/

my $string = '/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/';

my ($match) = $string =~ m,
 ^ # Beginning of string.
 / # Skip over the first '/'.
 [^/]* # Skip over anything that's not a '/'.
 / # Until the next '/'. Skip over that too.
 \*# Skip over the literal '*' character.
 ([ACGT]+) # Now capture the sequence we want.
 ,x;

print $match, "\n";

__END__

Output:

ACGACGGGTTCAAGGCAG

IF the '*' characters literally delimit the parts that you want
(AND not the parts that you don't want) then that's even easier:

#!/usr/bin/perl

use strict;
use warnings;

my $string = '/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/';

my ($match) = $string =~ /\*([ACGT]+)/;

print $match, "\n";

__END__

This produces the same output with this sample string. Without
seeing the real data it's hard to speculate. There might be a
better way. You need to know the specifications of the data
you're processing if you want to reliably process it
automatically. We need to know this to help you do it too.

   o o o o

A lot of people seem to post about this same type of data. I'd be
surprised if nobody has written CPAN modules for parsing the data
yet (and if not then perhaps it would be economical to do so).
Just saying...

Regards,






Re: Multiple matching of a group of characters

2012-10-01 Thread Brandon McCaig
On Mon, Oct 01, 2012 at 11:15:53PM +0100, Florian Huber wrote:
> Dear all,

Hello,

> $string = "/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/"

I would suggest that you show us the real data. I'm assuming that
'NOTNEEDED' is a placeholder for some data that you're not
interested in. Without knowing what that is we can't really say
for sure what is going on (though we can speculate; see below).

Note that you should be using the strict and warnings pragmas
(see below). The lack of 'my' here suggests that you probably
aren't.

> But when I do
> 
> $string =~ /[ACGT]/;
> 
> it matches only the last letter, i.e. "G". Why doesn't it start
> at the beginning?

It isn't matching the last letter. You are probably making the
wrong assumption. This is common when you're having trouble with
code. Again, show us the 'NOTNEEDED' part. :)

> But it gets even better, I figured that adding the greedy *
> should help:
> 
> $string =~ /[ACGT]*/;
> 
> and now it doesn't match anything. Shouldn't it try to match as
> many times as possible?

It should match at least the once that you saw earlier (assuming
the same data).

> My confusion was complete when I tried
> 
> $string =~ /[ACGT]{5}/;
> 
> now it matches 5 letters, but this time from the beginning,
> i.e.: ACGAC.

I'm guessing that the first 'NOTNEEDED' contains a 'G'. That
would explain the first match. The second result is nonesense
with the data we've seen. :-/ If 'NOTNEEDED' doesn't contain a
string at least 5 characters in length composed only of 'A', 'C',
'G', or 'T' then that would explain this last result.

> I fail to understand that behaviour. I checked the Perl
> documentation a bit and I sort of understand why /[ACGT]/ only
> matches one letter only (but not why it starts at the end).
> However, I'm simply puzzled at the other things.

As said, provide us with a full (minimal) program to demonstrate
the problems you're having if your problems persist.

Assuming 'NOTNEEDED' cannot contain '/' characters then you may
need to include those in your pattern to make sure you match the
parts you want. You will probably want to use captures for that
(see perldoc perlre). To understand the below program you will
also need to understand the /x modifier (again see perldoc
perlre).

#!/usr/bin/perl

use strict;   # <---Make sure you have these.
use warnings; # <--/

my $string = '/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/';

my ($match) = $string =~ m,
^ # Beginning of string.
/ # Skip over the first '/'.
[^/]* # Skip over anything that's not a '/'.
/ # Until the next '/'. Skip over that too.
\*# Skip over the literal '*' character.
([ACGT]+) # Now capture the sequence we want.
,x;

print $match, "\n";

__END__

Output:

ACGACGGGTTCAAGGCAG

IF the '*' characters literally delimit the parts that you want
(AND not the parts that you don't want) then that's even easier:

#!/usr/bin/perl

use strict;
use warnings;

my $string = '/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/';

my ($match) = $string =~ /\*([ACGT]+)/;

print $match, "\n";

__END__

This produces the same output with this sample string. Without
seeing the real data it's hard to speculate. There might be a
better way. You need to know the specifications of the data
you're processing if you want to reliably process it
automatically. We need to know this to help you do it too.

  o o o o

A lot of people seem to post about this same type of data. I'd be
surprised if nobody has written CPAN modules for parsing the data
yet (and if not then perhaps it would be economical to do so).
Just saying...

Regards,


-- 
Brandon McCaig  
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bamccaig.com/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'



signature.asc
Description: Digital signature


Re: Multiple matching of a group of characters

2012-10-01 Thread Andy Bach
On Mon, Oct 1, 2012 at 5:15 PM, Florian Huber  wrote:
>
> My confusion was complete when I tried
>
> $string =~ /[ACGT]{5}/;
>
> now it matches 5 letters, but this time from the beginning, i.e.: ACGAC.

>I'm trying to extract a DNA sequence out of a larger string, i.e. the string 
>is of the following structure:
$string = "/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/"
> But when I do
$string =~ /[ACGT]/;
> it matches only the last letter, i.e. "G". Why doesn't it start at the 
> beginning?

$ cat /tmp/g.pl
$string = "/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/";

## match and replace w/ and 'X'
if ( $string =~ s/([ACGT])/X/ ) {
  print "Matched: $1 in $string\n";
}
Macintosh-3:~ afbach$ perl /tmp/g.pl
Matched: T in /NOXNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/

The square brackets create a character class "either A or C or G or T"
and take up one position. "*" makes it zero or more, "+" one or more
and "{5}" means "exactly 5" but of any of those.

If I'm understanding you, you want the sequence of [ACGT]s demarked by
non-ACGTs.   While your example has the /* ... */ markers (so
if ( $string =~ m#/\*([ACGT]+)\*/# ) {

would work) I doubt that's your data. Is the string you want the
sequence of only ACGTs? This sort of works:
if ( $string =~ m/[^ACGT]([ACGT]+)[^AGCT]/ ) {
  print "Matched: $1 in $string\n";
}

but 


-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Multiple matching of a group of characters

2012-10-01 Thread Jim Gibson

On Oct 1, 2012, at 3:15 PM, Florian Huber wrote:

> Dear all,
> 
> I'm trying to extract a DNA sequence out of a larger string, i.e. the string 
> is of the following structure:
> 
> $string = "/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/"
> 
> But when I do
> 
> $string =~ /[ACGT]/;
> 
> it matches only the last letter, i.e. "G". Why doesn't it start at the 
> beginning?

How do you know that it only matches the last letter. That pattern should match 
the first letter that is either A, C, G, or T. In the value shown for $string, 
that should be 'A'. Can you show us a minimal program that demonstrates 
matching the 'G' at the end of the string? We need to see the whole program to 
explain something so inexplicable.

> 
> But it gets even better, I figured that adding the greedy * should help:
> 
> $string =~ /[ACGT]*/;
> 
> and now it doesn't match anything. Shouldn't it try to match as many times as 
> possible?

Not necessarily. You are asking for the FIRST position where the pattern 
matches. Since an asterisk means "zero or more", the regular expression 
/[ACGT]*/ can match anywhere in any string. Therefore, since the regular 
expression matches at the first letter, the regular expression will declare a 
match and stop.

If you want to match "one or more" characters, then use /[ACGT]+/.

> 
> My confusion was complete when I tried
> 
> $string =~ /[ACGT]{5}/;
> 
> now it matches 5 letters, but this time from the beginning, i.e.: ACGAC.

The regular expression engine starts at the beginning of the string and tries 
all matches from left to right until it succeeds or fails. You can use greedy 
or non-greedy quantifiers or anchors, e.g. \A and \z, to modify this behavior.

> I fail to understand that behaviour. I checked the Perl documentation a bit 
> and I sort of understand why /[ACGT]/ only matches one letter only (but not 
> why it starts at the end). However, I'm simply puzzled at the other things.
> 



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Multiple matching of a group of characters

2012-10-01 Thread Florian Huber

Dear all,

I'm trying to extract a DNA sequence out of a larger string, i.e. the 
string is of the following structure:


$string = "/NOTNEEDED/*ACGACGGGTTCAAGGCAG*/NOTNEEDED/"

But when I do

$string =~ /[ACGT]/;

it matches only the last letter, i.e. "G". Why doesn't it start at the 
beginning?


But it gets even better, I figured that adding the greedy * should help:

$string =~ /[ACGT]*/;

and now it doesn't match anything. Shouldn't it try to match as many 
times as possible?


My confusion was complete when I tried

$string =~ /[ACGT]{5}/;

now it matches 5 letters, but this time from the beginning, i.e.: ACGAC.


I fail to understand that behaviour. I checked the Perl documentation a 
bit and I sort of understand why /[ACGT]/ only matches one letter only 
(but not why it starts at the end). However, I'm simply puzzled at the 
other things.


Thank you for your help!

Florian


Re: matching array elements from hash ?

2012-09-01 Thread Dr.Ruud

On 2012-08-20 00:18, John W. Krahn wrote:


print map exists $stud{ $_ } ? "$_ = $stud{ $_ }\n" : (), @names;


A map using a ternary with (), is like a grep:

  print "$_ = $stud{ $_ }\n" for grep exists $stud{ $_ }, @names;

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: matching array elements from hash ?

2012-08-20 Thread timothy adigun
Hi Andy,

On 8/20/12, Andy Bach  wrote:
> On Sun, Aug 19, 2012 at 4:48 PM, timothy adigun <2teezp...@gmail.com>
> wrote:
>> foreach my $match_value ( sort keys %stud ) {
>> print $match_value, "=", $stud{$match_value},
>>   $/ if $match_value ~~ @names;
>> }
>
> "smart match" is a Perl 6 (though it probably back ported to a Perl 5
> module?) invention allowing DWIM here - look through the whole array
> for this value.  In normal P5

 Smart Match is introduced in Perl 5.10.1. You find more details here:
http://perldoc.perl.org/perlop.html UNDER the subheading smartmatch Operator.

>  foreach my $match_value ( sort keys %stud ) {
>  print "$match_value = $stud{$match_value}\n"
>if grep $match_value @names;

  Smart Match is a lot faster than grep, "for-loop" and probably
"first" from List::Util Module in Perl.
While I commented on why using for loop for this kind of problem is
not effective, John's solution using map{...} is more like it and alot
better.

> }
>
> but that means you're going through @names once for each element of
> stud.  Sounds like a job for a Schwartzian Transform (google it)  -
> create another hash w/ @names as the keys:
> my %match_keys;
> # use a hash slice to turn elements into keys
> @match_keys{@names} =  @names;
>
> and then
>  foreach my $match_value ( sort keys %stud ) {
> print "$match_value = $stud{$match_value}\n"
> if defined $match_keys{$match_value};
> }
>
> I used 'defined' in case one of your names was a zero or something.
> May be a 'better' route depending upon the size of the data and/or how
> often you need to check for that name as a key.
> --
>
> a
>
> Andy Bach,
> afb...@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
Tim

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: matching array elements from hash ?

2012-08-20 Thread timothy adigun
Hi,

> Hi Tim,
>
> Thanks, i tried to run the code, but get the error as below. Any thing i am
> missing line 17.

What version of Perl are you using?
For smart matching to work you must have Perl 5.10.1  Up (the 5.10.0
version behaved differently).

>  syntax error at ./match2.pl line 17, near "$match_value ~"
> Execution of ./match2.pl aborted due to compilation errors.
>
>  more match2.pl
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my @names = qw(abc def ghi jky);
>
> my %stud = (
> abc => "34",
> nba => "99",
> def => "24",
> ghi => "33",
> );
>
> foreach my $match_value ( sort keys %stud ) {
> print $match_value, "=", $stud{$match_value},
>   $/ if $match_value ~~ @names;
> }
>
>
>> --
>> Tim
>>
>


-- 
Tim

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: matching array elements from hash ?

2012-08-20 Thread Jim Gibson
The "smart match" operator (~~) was introduced in Perl 5.10. If you are using a 
Perl earlier than that, you will get a syntax error.

See

 perldoc perl5100delta
 perldoc perlsyn

and search for "Smart".


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: matching array elements from hash ?

2012-08-20 Thread Andy Bach
On Sun, Aug 19, 2012 at 4:48 PM, timothy adigun <2teezp...@gmail.com> wrote:
> foreach my $match_value ( sort keys %stud ) {
> print $match_value, "=", $stud{$match_value},
>   $/ if $match_value ~~ @names;
> }

"smart match" is a Perl 6 (though it probably back ported to a Perl 5
module?) invention allowing DWIM here - look through the whole array
for this value.  In normal P5
 foreach my $match_value ( sort keys %stud ) {
 print "$match_value = $stud{$match_value}\n"
   if grep $match_value @names;
}

but that means you're going through @names once for each element of
stud.  Sounds like a job for a Schwartzian Transform (google it)  -
create another hash w/ @names as the keys:
my %match_keys;
# use a hash slice to turn elements into keys
@match_keys{@names} =  @names;

and then
 foreach my $match_value ( sort keys %stud ) {
print "$match_value = $stud{$match_value}\n"
if defined $match_keys{$match_value};
}

I used 'defined' in case one of your names was a zero or something.
May be a 'better' route depending upon the size of the data and/or how
often you need to check for that name as a key.
-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: matching array elements from hash ?

2012-08-20 Thread jet speed
Thanks  John, worked as a treat. Appreciate it.


On Sun, Aug 19, 2012 at 11:18 PM, John W. Krahn  wrote:

> jet speed wrote:
>
>> Hi All,
>>
>
> Hello,
>
>
>  Is there a way to find  matching array elements from hash.
>>
>> ex:
>>
>> @names = ( abc. def. ghi, jky; );
>>
>> %stud  = (
>> " abc" =>" 34",
>> "nba" =>"99",
>> "def" =>"24",
>> "ghi"=>  "33");
>>
>> How can i go throught each elements of has %stud and print the matching
>> array value in this case
>>
>> abc =34
>> def=24
>>
>
> $ perl -e'
> my @names = qw( abc def ghi jky );
> my %stud  = (
>
> abc => 34,
> nba => 99,
> def => 24,
> ghi => 33,
> );
> print map exists $stud{ $_ } ? "$_ = $stud{ $_ }\n" : (), @names;
> '
> abc = 34
> def = 24
> ghi = 33
>
>
>
>
> John
> --
> Any intelligent fool can make things bigger and
> more complex... It takes a touch of genius -
> and a lot of courage to move in the opposite
> direction.   -- Albert Einstein
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: matching array elements from hash ?

2012-08-20 Thread jet speed
On Sun, Aug 19, 2012 at 10:48 PM, timothy adigun <2teezp...@gmail.com>wrote:

> Hi,
>
> Please, Check my comments below.
>
> On 8/19/12, jet speed  wrote:
> > Hi All,
> >
> > Is there a way to find  matching array elements from hash.
> >
> > ex:
> >
> > @names = ( abc. def. ghi, jky; );
>
>   The above should be @names=("abc","def","ghi","jky");
> OR
>@names=qw(abc def ghi jky); OR other variant but what you have written.
> >
> > %stud  = (
> > " abc" =>" 34",
> > "nba" =>"99",
> > "def" =>"24",
> > "ghi"=> "33");
>
>   You really don't on most cases what to quote the keys in the hash,
> since you are using the FAT arrow, which already does that, except for
> some few cases.
>
>   So, you have this:
>  my %stud = (
> abc => "34",
> nba => "99",
> def => "24",
> ghi => "33"
> );
>
> > How can i go throught each elements of has %stud and print the matching
> > array value in this case
> >
>   You can use smart-match "~~" with a foreach loop, like so:
>
> use warnings;
> use strict;
>
> my @names = qw(abc def ghi jky);
>
> my %stud = (
> abc => "34",
> nba => "99",
> def => "24",
> ghi => "33",
> );
>
> foreach my $match_value ( sort keys %stud ) {
> print $match_value, "=", $stud{$match_value},
>   $/ if $match_value ~~ @names;
> }
> __END__
>
> abc=34
> def=24
> ghi=33
>
> Warning: Though it might not be so effective if you have to loop
> through a long data list. You might take a look at the the function
> first in module List::Util.
>
> > abc =34
> > def=24
> >
>  ghi=33 will also match.
>
> Hope this helps.
> > Thanks
> > Sj
> >
>
>
Hi Tim,

Thanks, i tried to run the code, but get the error as below. Any thing i am
missing line 17.

 syntax error at ./match2.pl line 17, near "$match_value ~"
Execution of ./match2.pl aborted due to compilation errors.

 more match2.pl
#!/usr/bin/perl

use warnings;
use strict;

my @names = qw(abc def ghi jky);

my %stud = (
abc => "34",
nba => "99",
def => "24",
ghi => "33",
);

foreach my $match_value ( sort keys %stud ) {
print $match_value, "=", $stud{$match_value},
  $/ if $match_value ~~ @names;
}


> --
> Tim
>


Re: matching array elements from hash ?

2012-08-19 Thread John W. Krahn

jet speed wrote:

Hi All,


Hello,


Is there a way to find  matching array elements from hash.

ex:

@names = ( abc. def. ghi, jky; );

%stud  = (
" abc" =>" 34",
"nba" =>"99",
"def" =>"24",
"ghi"=>  "33");

How can i go throught each elements of has %stud and print the matching
array value in this case

abc =34
def=24


$ perl -e'
my @names = qw( abc def ghi jky );
my %stud  = (
abc => 34,
nba => 99,
def => 24,
ghi => 33,
);
print map exists $stud{ $_ } ? "$_ = $stud{ $_ }\n" : (), @names;
'
abc = 34
def = 24
ghi = 33




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: matching array elements from hash ?

2012-08-19 Thread timothy adigun
Hi,

Please, Check my comments below.

On 8/19/12, jet speed  wrote:
> Hi All,
>
> Is there a way to find  matching array elements from hash.
>
> ex:
>
> @names = ( abc. def. ghi, jky; );

  The above should be @names=("abc","def","ghi","jky");
OR
   @names=qw(abc def ghi jky); OR other variant but what you have written.
>
> %stud  = (
> " abc" =>" 34",
> "nba" =>"99",
> "def" =>"24",
> "ghi"=> "33");

  You really don't on most cases what to quote the keys in the hash,
since you are using the FAT arrow, which already does that, except for
some few cases.

  So, you have this:
 my %stud = (
abc => "34",
nba => "99",
def => "24",
ghi => "33"
);

> How can i go throught each elements of has %stud and print the matching
> array value in this case
>
  You can use smart-match "~~" with a foreach loop, like so:

use warnings;
use strict;

my @names = qw(abc def ghi jky);

my %stud = (
abc => "34",
nba => "99",
def => "24",
ghi => "33",
);

foreach my $match_value ( sort keys %stud ) {
print $match_value, "=", $stud{$match_value},
  $/ if $match_value ~~ @names;
}
__END__

abc=34
def=24
ghi=33

Warning: Though it might not be so effective if you have to loop
through a long data list. You might take a look at the the function
first in module List::Util.

> abc =34
> def=24
>
 ghi=33 will also match.

Hope this helps.
> Thanks
> Sj
>


-- 
Tim

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




matching array elements from hash ?

2012-08-19 Thread jet speed
Hi All,

Is there a way to find  matching array elements from hash.

ex:

@names = ( abc. def. ghi, jky; );

%stud  = (
" abc" =>" 34",
"nba" =>"99",
"def" =>"24",
"ghi"=> "33");

How can i go throught each elements of has %stud and print the matching
array value in this case

abc =34
def=24

Thanks
Sj


Re: capture more than one regex matching (oneliner)

2012-03-29 Thread John W. Krahn

Christian wrote:

Hi,


Hello,


is there easy  way to capture more than one matching and print this
out?
In the example below the first matching is suppressed.

cat file |  perl -nle '/p2=(.+)(?=&p3)/&&   /p13=(.+)(?=&p14)/&&
print  $. . ";" .  $1 . ";". $2'



perl -nle'/(?=.*p2=(.+)&p3)(?=.*p13=(.+)&p14)/ && print "$.;$1;$2"' file




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




capture more than one regex matching (oneliner)

2012-03-29 Thread Christian
Hi,

is there easy  way to capture more than one matching and print this
out?
In the example below the first matching is suppressed.

cat file |  perl -nle '/p2=(.+)(?=&p3)/ &&  /p13=(.+)(?=&p14)/ &&
print  $. . ";" .  $1 . ";". $2'

Thanks for any help.
Christian


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: multiple pattern matching

2012-03-08 Thread Rob Dixon

On 08/03/2012 17:03, sunita.prad...@emc.com wrote:

Hi All

I have one output of one my command like :

$output = "Step 155 of 171 steps.Executing.
 Step 168 of 171 steps.Executing.
 Step 171 of 171 steps.Executing.
 Local:  COMMITDone.

   New symdev:  2552
   New symdev:  2553
 Terminating the configuration change session..Done." ;

I need to get those numbers like 2552 , 2553 .

I am applying following patter matching logic but it does not get me the  1st 
number (2552) .

@devs = $output =~ m/New symdev:\s*([0-9A-Fa-f]{4})/gis;
 print $output,"\n";
 print "Devices>>>>  @devs<<<<<<<<<\n";


Could you please check what is going  wrong in  the above lines ?


I am no sure what you mean Sunita. I have run your program and the
output is

  Devices >>>> 2552 2553 <<<<<<<<<

as I would expect.

All I can see is that you don't need the /s modifier as you aren't using
/./ anywhere in your regex; and with the /i modifier you can shorten
your character class to [0-9a-f], or you could write [[:xdigit:]].
Neither of these will change the functionality of your program though -
I think it's fine.

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: multiple pattern matching

2012-03-08 Thread Chris Stinemetz
On Thu, Mar 8, 2012 at 11:03 AM,   wrote:
> Hi All
>
> I have one output of one my command like :
>
> $output = "Step 155 of 171 
> steps.Executing.
>    Step 168 of 171 steps.Executing.
>    Step 171 of 171 steps.Executing.
>    Local:  COMMITDone.
>
>      New symdev:  2552
>      New symdev:  2553
>    Terminating the configuration change session..Done." ;
>
> I need to get those numbers like 2552 , 2553 .
>
> I am applying following patter matching logic but it does not get me the  1st 
> number (2552) .
>
> @devs = $output =~ m/New symdev:\s*([0-9A-Fa-f]{4})/gis;
>                        print $output,"\n";
>                        print "Devices >>>> @devs <<<<<<<<<\n";
>
>
> Could you please check what is going  wrong in  the above lines ?
>

My approach would be to read it line by line with a while loop. There
may be a better way of achieving your goal.

#!/usr/bin/perl
use warnings;
use strict;

while (  ) {
  if ( $_ =~ m/New symdev:\s+(\d+)/ ) {
print "$1\n";
  }
}


__DATA__
my $output = "Step 155 of 171
steps.Executing.
   Step 168 of 171 steps.Executing.
   Step 171 of 171 steps.Executing.
   Local:  COMMITDone.

 New symdev:  2552
 New symdev:  2553
   Terminating the configuration change session..Done." ;

###OUTPUT###
2552
2553

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




multiple pattern matching

2012-03-08 Thread Sunita.Pradhan
Hi All

I have one output of one my command like :

$output = "Step 155 of 171 steps.Executing.
Step 168 of 171 steps.Executing.
Step 171 of 171 steps.Executing.
Local:  COMMITDone.

  New symdev:  2552
  New symdev:  2553
Terminating the configuration change session..Done." ;

I need to get those numbers like 2552 , 2553 .

I am applying following patter matching logic but it does not get me the  1st 
number (2552) .

@devs = $output =~ m/New symdev:\s*([0-9A-Fa-f]{4})/gis;
print $output,"\n";
print "Devices >>>> @devs <<<<<<<<<\n";


Could you please check what is going  wrong in  the above lines ?


Thanks
Sunita


RE: Matching Greek letters in UTF-8 file

2011-10-10 Thread Hamann, T.D. (Thomas)
Many thanks for the replies. Reading the documentation, it looks like it's a 
bit more complicated than I had hoped.

On the other hand, I realized that for my purpose (removing unwanted hyphens 
from an OCR'ed document), I don't actually need to match the greek letters, 
because they occur in two unique formats throughout the whole document (which 
should match \w- and -\w- ).

Thomas



Van: Brian Fraser [frase...@gmail.com]
Verzonden: donderdag 29 september 2011 16:59
Aan: John Delacour
CC: beginners@perl.org
Onderwerp: Re: Matching Greek letters in UTF-8 file

On Thu, Sep 29, 2011 at 10:58 AM, John Delacour wrote:

> use encoding 'utf-8';
>
>

Nitpick: Please don't use this, as encoding is broken. use utf8; and use
open qw< :std :encoding(UTF-8) >; should make do for a replacement.

To the original poster, please note that there's a bit of a difference in
case-insensitive matching (i.e. using /i) -- newer versions of Perl do full
casefolding (so \N{GREEK CAPITAL LETTER ALPHA WITH PSILI AND PROSGEGRAMMENI}
matches \N{GREEK SMALL LETTER ALPHA WITH PSILI}\N{GREEK SMALL LETTER IOTA}),
whereas older versions don't. So if you need to do that, I'd recommend
giving the docs a thorough read. Also this:
http://98.245.80.27/tcpc/OSCON2011/upr.html
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: smart matching

2011-10-06 Thread John SJ Anderson
On Thu, Oct 6, 2011 at 03:10, Shlomi Fish  wrote:
> Furthermore, I think you can only put when inside given and not arbitrary 
> code,
> and that "when()" operates on the datum that was given to given().

I don't think there's any restriction on what code can be inside the
'given' block -- at least, there's nothing in 'perlsyn' saying you
can't have other code in there.

You are correct that 'given()' sets up a (lexically scoped, I guess?)
$_ equal to the argument that it is given -- which is a copy, not an
alias -- and the smart matches in when() will then use that $_ -- but
if you have a 'when()' that doesn't check $_, then you're fine. See
sample code below.

Note: that is not say that any of this is *advisable* -- it's not,
unless you really really need to. (You don't really really need to, in
case you're wondering.) You're probably better off only having
'when()' blocks inside a 'given()' block, and you're probably better
off keeping the conditions in your 'when()' tests nice and simple.
You'll thank yourself down the road.

Sample code:

#! /usr/bin/env perl

use strict;
use warnings;
use feature 'switch';

$_ = 'outside';

my $foo = 'foo';

given( $foo ) {
  when( 1 == 1 ) { print "INIT\n" ; continue }

  when( 'bar' )  { print "BAR: $foo $_\n" }

  $foo = 'bar';

  when( 'foo' )  { print "FOO: $foo $_\n" }

  $foo = 'baz';

  when( 'outside' ) { print "OUT: $foo $_\n" }

  default { print "DEF: $foo $_\n" }
}

print;

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: smart matching

2011-10-06 Thread timothy adigun
Hi Chris,
 I think you will need to work on your code to make it better or maybe this
is just a dirty codes for practice. If not, there are some stuff you may
have to weed out...some of which Shlomi Fish mentioned.

However, to make your code work as you suppose, please check this little
addition and you can take it up from there.


#!/usr/bin/perl

use warnings;
use strict;
use 5.010;

say "Checking the number <$ARGV[0]>";

my $favorite = 62;

given( $ARGV[0] ) {
   when( ! /^\d+$/ ) { say "Not a number!" }

   my @divisors = divisors( $ARGV[0] );
my $val=2;   #added test value
   when( @divisors ~~  /$val/) { # 2 is in @divisors ,/$val/ check it
   say "$_ is even";
   continue;
   }

   when( !( @divisors ~~/$val/ ) ) { # 2 isn't in @divisors /$val/ check
it
   say "$_ is odd!";
   continue;
   }

   when( @divisors ~~ /$favorite/ ) { # /$favorite/ check it
   say "$_ is divisible by my favorite number";
   continue;
   }

   when( /$favorite/ ) { # $_ ~~ $favorite
   say "$_ is my favorite number";
   continue;
   }

   my @empty;
   when( @divisors ~~ @empty ) { say "Number is prime" }

   default { say "$_ is divisible by @divisors" }
   }

sub divisors {
   my $number = shift;

   my @divisors = ();
   foreach my $divisor ( 2 .. ($ARGV[0]/2 + 1) ) {
   push @divisors, $divisor unless $number % $divisor;
   }

return @divisors;
}


Regards,
timothy


Re: smart matching

2011-10-06 Thread Shlomi Fish
On Wed, 5 Oct 2011 23:44:21 -0500
Chris Stinemetz  wrote:

> trying to learn smart matching in an exercise.
> 
> Why does this program output "odd" when I input an even number?
> 

A few comments on your code.

> Thank you,
> 
> Chris
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> use 5.010;
> 
> say "Checking the number <$ARGV[0]>";
> 

You're using $ARGV[0] several times (duplicate code) and it's a positional
argument:

http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments

Better unpack it as:

my $number = shift(@ARGV);

> my $favorite = 62;
> 
> given( $ARGV[0] ) {
> when( ! /^\d+$/ ) { say "Not a number!" }
> 
> my @divisors = divisors( $ARGV[0] );
> 

Returns arrays as references is preferable.

Furthermore, I think you can only put when inside given and not arbitrary code,
and that "when()" operates on the datum that was given to given().

> when( @divisors ~~ 2 ) { # 2 is in @divisors
> say "$_ is even";
> continue;
> }
> 
> when( !( @divisors ~~ 2 ) ) { # 2 isn't in @divisors
> say "$_ is odd!";
> continue;
> }
> 
> when( @divisors ~~ $favorite ) {
> say "$_ is divisible by my favorite number";
> continue;
> }
> 
> when( $favorite ) { # $_ ~~ $favorite
> say "$_ is my favorite number";
> continue;
> }
> 
> my @empty;
> when( @divisors ~~ @empty ) { say "Number is prime" }
> 
> default { say "$_ is divisible by @divisors" }
> }
> 

Your indentation is erratic.

> sub divisors {
> my $number = shift;
> 
> my @divisors = ();

No need for the «= ()» here. An array is initialised as empty by default.

> foreach my $divisor ( 2 .. ($ARGV[0]/2 + 1) ) {

Do you want to operate on "$number" or on "$ARGV[0]"?

> push @divisors, $divisor unless $number % $divisor;
> }
>

I got a "WTF?" moment when I saw this unless construct. I'd write it as:

if ($number % $divisor == 0)
{
push @divisors, $divisor;
}

Regards,

Shlomi Fish
 
> return @divisors;
> 



-- 
-
Shlomi Fish   http://www.shlomifish.org/
Stop Using MSIE - http://www.shlomifish.org/no-ie/

I used to be arrogant. Now I’m simply Perfect.
— one of Shlomi Fish’s relatives.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




smart matching

2011-10-05 Thread Chris Stinemetz
trying to learn smart matching in an exercise.

Why does this program output "odd" when I input an even number?

Thank you,

Chris

#!/usr/bin/perl

use warnings;
use strict;

use 5.010;

say "Checking the number <$ARGV[0]>";

my $favorite = 62;

given( $ARGV[0] ) {
when( ! /^\d+$/ ) { say "Not a number!" }

my @divisors = divisors( $ARGV[0] );

when( @divisors ~~ 2 ) { # 2 is in @divisors
say "$_ is even";
continue;
}

when( !( @divisors ~~ 2 ) ) { # 2 isn't in @divisors
say "$_ is odd!";
continue;
}

when( @divisors ~~ $favorite ) {
say "$_ is divisible by my favorite number";
continue;
}

when( $favorite ) { # $_ ~~ $favorite
say "$_ is my favorite number";
continue;
}

my @empty;
when( @divisors ~~ @empty ) { say "Number is prime" }

default { say "$_ is divisible by @divisors" }
}

sub divisors {
my $number = shift;

my @divisors = ();
foreach my $divisor ( 2 .. ($ARGV[0]/2 + 1) ) {
push @divisors, $divisor unless $number % $divisor;
}

return @divisors;

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Matching Greek letters in UTF-8 file

2011-09-29 Thread John Delacour

At 17:29 -0300 29/9/11, Brian Fraser wrote:


On Thu, Sep 29, 2011 at 4:03 PM, John Delacour  wrote:


Nitpick:  Why the upper-case charset name?


Uppercase is UTF-8-strict, while lowercase is the lax version that 
perl uses internally. Unless you are passing data from one perl 
program to another, and you are using illegal-UTF8-but-legal-UTFX 
(like if you define your own new characters beyond 10, which 
Perl allows but strict UTF-8 shouldn't), there's basically no good 
reason to use the lax version.


Right. Thank you for the explanation.  Perl has so affected my 
thinking that I'd forgotten that RFC 3629 names it upper case 'UTF-8'.


But if you include 'qw< :std :encoding(utf-8) >', is that not also 
using encoding?


...The :encoding() part actually refers to a layer provided by a 
module, not the encoding pragma.


Gotcha.


Yeah, it's a mess. :)


Yes, but thank goodness for Dan Kogai et al.  I'd sooner have them 
sort it out than do it myself!


JD



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Matching Greek letters in UTF-8 file

2011-09-29 Thread Brian Fraser
On Thu, Sep 29, 2011 at 4:03 PM, John Delacour wrote:

>
>
Nitpick:  Why the upper-case charset name?
>

Uppercase is UTF-8-strict, while lowercase is the lax version that perl uses
internally. Unless you are passing data from one perl program to another,
and you are using illegal-UTF8-but-legal-UTFX (like if you define your own
new characters beyond 10, which Perl allows but strict UTF-8 shouldn't),
there's basically no good reason to use the lax version.


>
> Interesting to hear that encoding is broken.  I came across a problem the
> other day wich I couldn't work out at all.  But if you include 'qw< :std
> :encoding(utf-8) >', is that not also using encoding?
>
>
Sorta, but not quite. use encoding ...; does a couple of different things,
and most of them not that well. Foremost it sets the source encoding to some
arbitrary value, but also the default encodings for IO streams -- whereas
use open ...; only sets the default encodings, with :std setting them for
STD(IN|ERR|OUT).
The :encoding() part actually refers to a layer provided by a module, not
the encoding pragma.

Yeah, it's a mess. :)


Re: Matching Greek letters in UTF-8 file

2011-09-29 Thread John Delacour

At 11:59 -0300 29/9/11, Brian Fraser wrote:


On Thu, Sep 29, 2011 at 10:58 AM, John Delacour 
 wrote:


use encoding 'utf-8';

Nitpick: Please don't use this, as encoding is broken. use utf8; and 
use open qw< :std :encoding(UTF-8) >; should make do for a 
replacement.


Nitpick:  Why the upper-case charset name?

Interesting to hear that encoding is broken.  I came across a problem 
the other day wich I couldn't work out at all.  But if you include 
'qw< :std :encoding(utf-8) >', is that not also using encoding?


Another thing I realized the other day is that a the problem that 
existed in Perl 5.10 and 5.12, even when I updated Encode for these 
versions, seemed to have been solved in 5.14.


JD

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Matching Greek letters in UTF-8 file

2011-09-29 Thread Brian Fraser
On Thu, Sep 29, 2011 at 10:58 AM, John Delacour wrote:

> use encoding 'utf-8';
>
>

Nitpick: Please don't use this, as encoding is broken. use utf8; and use
open qw< :std :encoding(UTF-8) >; should make do for a replacement.

To the original poster, please note that there's a bit of a difference in
case-insensitive matching (i.e. using /i) -- newer versions of Perl do full
casefolding (so \N{GREEK CAPITAL LETTER ALPHA WITH PSILI AND PROSGEGRAMMENI}
matches \N{GREEK SMALL LETTER ALPHA WITH PSILI}\N{GREEK SMALL LETTER IOTA}),
whereas older versions don't. So if you need to do that, I'd recommend
giving the docs a thorough read. Also this:
http://98.245.80.27/tcpc/OSCON2011/upr.html


Re: Matching Greek letters in UTF-8 file

2011-09-29 Thread John Delacour

At 11:42 + 29/9/11, Hamann, T.D. (Thomas) wrote:

I need to write a regex that matches any single Greek letter 
followed by a hyphen in a UTF-8 text file that is otherwise in 
English.


How can I match the Greek alphabet (lower and upper case)?


#!/usr/local/bin/perl
use strict;
use utf8;
use encoding 'utf-8';
$_ = "Ναυσικάα, τί νύ σ᾽ ὧ-δε μεθήμονα 
γείνατο μήτηρ;";

print $1 if /(\p{Greek}-)/;

This will match polyhinic Greek as well, as you will see if you put a 
hyphen after any letter in $_


JD

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Matching Greek letters in UTF-8 file

2011-09-29 Thread Hamann, T.D. (Thomas)
Hi,
 
I need to write a regex that matches any single Greek letter followed by a 
hyphen in a UTF-8 text file that is otherwise in English.
 
How can I match the Greek alphabet (lower and upper case)?
 
Thanks,
Thomas
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: pattern matching regex

2011-09-27 Thread Shawn H Corey

On 11-09-27 05:30 PM, Chris Stinemetz wrote:

Trying to match the what is contained in $what 3 consecutive times.

But I am getting the followoing error:

Use of uninitialized value $_ in pattern match (m//) at ./ex9-1.pl line 7.

The program:

#!/usr/bin/perl

use warnings;
use strict;

my $what = 'fred|barney';
if (/($what){3}/) {


# This is a short cut of:

if( $_ =~ m/($what){3}/ ){

# $_ has nothing in it


print $what;


# If you want what was matched:
if( m/((?:$what){3})/ ){
  print $1, "\n";


}




--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

"Make something worthwhile."  -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




pattern matching regex

2011-09-27 Thread Chris Stinemetz
Trying to match the what is contained in $what 3 consecutive times.

But I am getting the followoing error:

Use of uninitialized value $_ in pattern match (m//) at ./ex9-1.pl line 7.

The program:

#!/usr/bin/perl

use warnings;
use strict;

my $what = 'fred|barney';
if (/($what){3}/) {
print $what;
}

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: trouble matching words with parentheses using grep

2011-04-10 Thread C.DeRykus
On Apr 9, 1:04 pm, alanhag...@alanhaggai.org (Alan Haggai Alavi)
wrote:
> > ...
> > #!usr/bin/perl
>
> For increased portability, use the shebang #!/usr/bin/env perl
>

Hm,   portable only in limited situations, risky,
and always slower.

 From:
 http://www.webmasterkb.com/Uwe/Forum.aspx/perl/3968/env-perl-or-simply-perl

   Randal Schwartz's response from above thread:
   ...
   Seconded on the "reduced portability".  The shebang
   path has to be accurate.  Throwing "env" into the mix
   means that env has to exist at a known location.  Some
   systems don't have it, some system have it at /bin/env,
   and some systems have it at /usr/bin/env... so you're
   only portable within a subset of machines.

   Also, you're then also doing a double-launch.  First, the
   kernel launches env, then env has to wake up and figure
   out where Perl is, and then launch that.

  And, if that wasn't enough, you risk that your script will be
  run by a privately installed Perl when someone else runs
  it... which might not have the right version or the right
  installed modules.  An explicit #! path never depends on
  PATH, so that's not an issue.

  So, in general, avoid this strategy when you can.

--
Charles DeRykus





--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: trouble matching words with parentheses using grep

2011-04-09 Thread John W. Krahn

Mariano Loza Coll wrote:

Hello everyone,


Hello,


Here's what I need to do: I need to take each of the words in one
List1, search for their presence (or not) in a second List2 and make a
new list with all the words in both. These are lists of gene names,
which can often include numbers and symbols in addition to letters.

The very newbie code that I wrote performs well, but it's missing the
words that have parentheses in them (for instance "Su(var)2-5").
Below are examples of the lists that I'm working with, the code that
I'm using, and the output.

I realize that there may be a number of ways to do what I need to do
(most of them better, I bet), and I'd love to learn about them. But
now I'm mostly curious about why grep// cannot "see" words with
parentheses in either (or both lists). I suspect the trick may be
somehow escaping the (), but I tried a number of ways of doing that to
no avail.

Any help will be greatly appreciated! And as usual, if you ever have
questions about molecular biology and genetics, fire away - I'd love
to pay the favor back.

Thanks in advance,
Mariano

Example of List 1

numb
Dl
cad99C
ham
esg
Stat92E
Hh
l(2)Lg
neur
CG32150
sox15N
Su(var)2-5
E(spl)-m4
ci
brd
vvl

Example of List 2

Src42A
cad99C
ham
Hh
l(2)Lg
neur
sox15N
numb
ubx
Su(var)2-5
esg
E(spl)-m4
ci
ttk
egfr
brd
ocho
vvl
CG32150

## SCRIPT BEGINS
#!usr/bin/perl
use warnings;


use strict;



print "\nEnter path to List 1\n\n";
$path1 =; chomp $path1;

print "\nEnter path to List 2\n\n";
$path2 =; chomp $path2;

open (INPUT1, "$path1"); open (INPUT2, "$path2"); open (INPUT3, "$path3");


You should *always* verify that a file opened correctly:

open INPUT1, '<', $path1 or die "Cannot open '$path1' because: $!";
open INPUT2, '<', $path2 or die "Cannot open '$path2' because: $!";
open INPUT3, '<', $path3 or die "Cannot open '$path3' because: $!";



@array1 =;
@array2 =;

my ($array1,$array2,$in1and2);


You never use those variables anywhere.



my $ctr1 = 0;
while ($ctr1<  @array1){
if(grep(/^$array1[$ctr1]$/,@array2)){


Change that line to this to get your program to work correclty:

if ( grep $_ eq $array1[ $ctr1 ], @array2 ) {



push (@in1and2,$array1[$ctr1]);
}
$ctr1 +=1;
}

print "in 1= ".@array1."\nin 2= ".@array2."in 1 and 2= ".@in1and2."\n\nDone!";

exit;

##

The output is...

in 1= 16
in 2= 19
in 1 and 2= 11

Done!

...when it should have been...

in 1= 16
in 2= 19
in 1 and 2= 14

but it's not "seeing" l(2)Lg, Su(var)2-5, E(spl)-m4 in both lists...
(I know this based on troubleshooting)




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: trouble matching words with parentheses using grep

2011-04-09 Thread Alan Haggai Alavi

Hello Mariano,


use List::MoreUtils qw( any each_array );
I was experimenting and forgot to take off `each_array` from the import 
list. `each_array` is not used in the alternative solution and is hence 
not required.


Regards,
Alan Haggai Alavi.
--
The difference makes the difference

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: trouble matching words with parentheses using grep

2011-04-09 Thread Alan Haggai Alavi

Hello Mariano,


I realize that there may be a number of ways to do what I need to do
(most of them better, I bet), and I'd love to learn about them. But
now I'm mostly curious about why grep// cannot "see" words with
parentheses in either (or both lists). I suspect the trick may be
somehow escaping the (), but I tried a number of ways of doing that to
no avail.


Characters having specific meanings in regular expressions have to be 
escaped. You could either use the quotemeta function or enclose the 
regular expression within \Q and \E:


grep /^ \Q $array1[$ctr1] \E $/x, @array2;

Some comments about the code:


#!usr/bin/perl

For increased portability, use the shebang #!/usr/bin/env perl


use warnings;

Also use strict;.


$path1 =; chomp $path1;

Declare variables before they are used.


open (INPUT1, "$path1"); open (INPUT2, "$path2"); open (INPUT3, "$path3");

Try to use the 3-argument version of open.


my ($array1,$array2,$in1and2);

Avoid naming scalars and arrays (or hashes) the same.

An alternative solution:

=pod code

#!/usr/bin/env perl

use strict;
use warnings;

use File::Slurp qw( slurp );
use List::MoreUtils qw( any each_array );

my %path;
print "Enter path to list 1: ";
chomp( $path{'list_1'} =  );
print "Enter path to list 2: ";
chomp( $path{'list_2'} =  );

chomp( my @list_1 = slurp( $path{'list_1'} ) );
chomp( my @list_2 = slurp( $path{'list_2'} ) );

my @common_list;

for my $word (@list_1) {
any { $_ eq $word } @list_2 and push @common_list, $_;
}

printf "in 1 = %d
in 2 = %d
in 1 and 2 = %d\n", scalar @list_1, scalar @list_2, scalar @common_list;

=cut

> Any help will be greatly appreciated! And as usual, if you ever have
> questions about molecular biology and genetics, fire away - I'd love
> to pay the favor back.

Thank you.

Hope this message helps. :-)

Regards,
Alan Haggai Alavi.
--
The difference makes the difference

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




trouble matching words with parentheses using grep

2011-04-09 Thread Mariano Loza Coll
Hello everyone,

Here's what I need to do: I need to take each of the words in one
List1, search for their presence (or not) in a second List2 and make a
new list with all the words in both. These are lists of gene names,
which can often include numbers and symbols in addition to letters.

The very newbie code that I wrote performs well, but it's missing the
words that have parentheses in them (for instance "Su(var)2-5").
Below are examples of the lists that I'm working with, the code that
I'm using, and the output.

I realize that there may be a number of ways to do what I need to do
(most of them better, I bet), and I'd love to learn about them. But
now I'm mostly curious about why grep// cannot "see" words with
parentheses in either (or both lists). I suspect the trick may be
somehow escaping the (), but I tried a number of ways of doing that to
no avail.

Any help will be greatly appreciated! And as usual, if you ever have
questions about molecular biology and genetics, fire away - I'd love
to pay the favor back.

Thanks in advance,
Mariano

Example of List 1

numb
Dl
cad99C
ham
esg
Stat92E
Hh
l(2)Lg
neur
CG32150
sox15N
Su(var)2-5
E(spl)-m4
ci
brd
vvl

Example of List 2

Src42A
cad99C
ham
Hh
l(2)Lg
neur
sox15N
numb
ubx
Su(var)2-5
esg
E(spl)-m4
ci
ttk
egfr
brd
ocho
vvl
CG32150

## SCRIPT BEGINS
#!usr/bin/perl
use warnings;

print "\nEnter path to List 1\n\n";
$path1 = ; chomp $path1;

print "\nEnter path to List 2\n\n";
$path2 = ; chomp $path2;

open (INPUT1, "$path1"); open (INPUT2, "$path2"); open (INPUT3, "$path3");
@array1 = ;
@array2 = ;

my ($array1,$array2,$in1and2);

my $ctr1 = 0;
while ($ctr1 < @array1){
if(grep(/^$array1[$ctr1]$/,@array2)){
push (@in1and2,$array1[$ctr1]);
}
$ctr1 +=1;
}

print "in 1= ".@array1."\nin 2= ".@array2."in 1 and 2= ".@in1and2."\n\nDone!";

exit;

##

The output is...

in 1= 16
in 2= 19
in 1 and 2= 11

Done!

...when it should have been...

in 1= 16
in 2= 19
in 1 and 2= 14

but it's not "seeing" l(2)Lg, Su(var)2-5, E(spl)-m4 in both lists...
(I know this based on troubleshooting)

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Smart matching

2011-01-18 Thread Vladimir D Belousov

19.01.2011 0:43, Brian Fraser пишет:

The smart match is no longer commutative - That was removed after 5.10.1, I
think.

http://www.learning-perl.com/?p=32
http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail

Brian.


Thank you!


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Smart matching

2011-01-18 Thread Brian Fraser
The smart match is no longer commutative - That was removed after 5.10.1, I
think.

http://www.learning-perl.com/?p=32
http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail

Brian.


Re: Smart matching

2011-01-18 Thread Vladimir D Belousov

19.01.2011 0:09, Uri Guttman пишет:
but why don't you just call exists on the hash key? there is no win to 
using smart matching for that. it is included for consistancy but it 
isn't needed for hash keys.




I just want to understand how does it work :)


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Smart matching

2011-01-18 Thread Uri Guttman
>>>>> "VDB" == Vladimir D Belousov  writes:

  VDB> I'm trying to check whether the given key exists in the hash.

smart matching is powerful and cool but why don't you just call exists
on the hash key? there is no win to using smart matching for that. it is
included for consistancy but it isn't needed for hash keys.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Smart matching

2011-01-18 Thread Vladimir D Belousov

I'm trying to check whether the given key exists in the hash.
The simple example:

use feature ':5.10';

my %a = (a => 1, b => 2);
say %a ~~ 'a' ? 'YES' : 'NO';# says NO -- why?
say %a ~~ 'c' ? 'YES' : 'NO';# says NO
say 'a' ~~ %a ? 'YES' : 'NO';# says YES
say 'c' ~~ %a ? 'YES' : 'NO';# says NO

There is a description of smart mathing operator:
http://search.cpan.org/~rgarcia/perl-5.10.0-RC1/pod/perlsyn.pod#Smart_matching_in_detail
"...The behaviour of a smart match depends on what type of thing its 
arguments are. It is always commutative, i.e. |$a ~~ $b| behaves the 
same as |$b ~~ $a|..."


Why is the result of %a ~~ 'a' differs from the result of 'a' ~~ %a ?



Re: regex for matching Google URLs

2011-01-18 Thread Octavian Rasnita

From: "Uri Guttman" 

"AM" == Alexey Mishustin  writes:


 AM> I used brackets not for storing but for combining in order to use the
 AM> combined patterns in alternation.

the point is parens (the correct term. brackets are []) is they will
grab the match inside them and store it in $1 and friends. grouping
without grabbing is more efficient and also tells the reader (that
person again! :) that they shouldn't look for using $1 (or whatver
number) after this regex is used.

 AM> Oops. Evidently, I was wrong in this combining... I meant

 AM> (imgres)

 AM> OR

 AM> (images)

 AM> OR

 AM> (products)

nope. you mean (:?imgres|images|products).

uri



The correct syntax is (?:imgres|images|products).

The wrong syntax appeared in 2 messages and it might cause confusion.

Octavian


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regex for matching Google URLs

2011-01-18 Thread Alexey Mishustin

1/18/2011, "Uri Guttman"  вы писали:

>> "AM" == Alexey Mishustin  writes:
>
>  AM> 1/18/2011, "Uri Guttman"  вы писали:
>
>  >>> "AM" == Alexey Mishustin  writes:
>  >> 
>  AM> I used brackets not for storing but for combining in order to use the
>  AM> combined patterns in alternation.
>  >> 
>  >> the point is parens 
>
>  >> (the correct term. brackets are [])
>  AM> Eh... Useful correction.
>
>  AM> And what is the correct term. for {} ?
>
>braces.
>
>() are parentheses or parens for short
>[] are (square) brackets
>{} are (curly) braces
>
>i don't know the russian versions! :)

() круглые скобки - literally, round brackets
[]  квадратные скобки - literally, square brackets
{} фигурные скобки - literally, figured brackets

:)

--

Regards,
Alex

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regex for matching Google URLs

2011-01-18 Thread Uri Guttman
> "AM" == Alexey Mishustin  writes:

  AM> 1/18/2011, "Uri Guttman"  вы писали:

  >>> "AM" == Alexey Mishustin  writes:
  >> 
  AM> I used brackets not for storing but for combining in order to use the
  AM> combined patterns in alternation.
  >> 
  >> the point is parens 

  >> (the correct term. brackets are [])
  AM> Eh... Useful correction.

  AM> And what is the correct term. for {} ?

braces.

() are parentheses or parens for short
[] are (square) brackets
{} are (curly) braces

i don't know the russian versions! :)

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regex for matching Google URLs

2011-01-18 Thread Uri Guttman
> "AM" == Alexey Mishustin  writes:

  AM> 1/18/2011, "Alexey Mishustin"  вы писали:

  >> I meant
  >> 
  >> (imgres)
  >> 
  >> OR
  >> 
  >> (images)
  >> 
  >> OR
  >> 
  >> (products)

  AM> Uri wrote the correct alternation for that:

  AM> (imgres|images|products)

  AM> So, I should write

  AM> /(www\.){0,1}(google\.).*\/(imgres|images|products)\?{0,1}/

you are still grabbing and not just grouping. and of course you are
still using {0,1} instead of ?

and finally you are using / for the delimiter when {} looks much better
when you have / in the regex.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regex for matching Google URLs

2011-01-18 Thread Alexey Mishustin

1/18/2011, "Uri Guttman"  вы писали:

>> "AM" == Alexey Mishustin  writes:
>
>  AM> I used brackets not for storing but for combining in order to use the
>  AM> combined patterns in alternation.
>
>the point is parens 

>(the correct term. brackets are [])
Eh... Useful correction.

And what is the correct term. for {} ?

>is they will
>grab the match inside them and store it in $1 and friends. grouping
>without grabbing is more efficient and also tells the reader (that
>person again! :) that they shouldn't look for using $1 (or whatver
>number) after this regex is used.
>
>  AM> Oops. Evidently, I was wrong in this combining... I meant
>
>  AM> (imgres)
>
>  AM> OR
>
>  AM> (images)
>
>  AM> OR
>
>  AM> (products)
>
>nope. you mean (:?imgres|images|products).

Yes, I see it now. Thanks again.

--

Regards,
Alex

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regex for matching Google URLs

2011-01-18 Thread Alexey Mishustin

1/18/2011, "Uri Guttman"  вы писали:

>> "AM" == Alexey Mishustin  writes:

>  AM> /(www.){0,1}(google\.).*\/(imgres)|(images)|(products)\?{0,1}/
>  >> 
>  >> {0,1} is just ? by itself.
>
>  AM> Yes, I know. But I like the {a,b} syntax more :) It's more uniform than
>  AM> ?,+,* etc.
>
>it is noisier and more people know the shortcuts. code so other people
>can read your code as it is for them, not yourself.

It's interesting, do most people here think so?

>  >> you don't need to grab things that are not used later on. also why grab
>  >> each trailing word separately which means it will be hard to tell what
>  >> word was there.
>
>  AM> Where did I grab things that are not used later? What do you mean by
>  AM> trailing word?
>
>look in perldoc perlre and look at the difference between (foo) and
>(:?foo). 

"This may substantially slow your program. Perl uses the same mechanism
to produce $1, $2, etc, so you also pay a price for each pattern that
contains capturing parentheses. (To avoid this cost while retaining the
grouping behaviour, use the extended regular expression (?: ... )
instead.)"

Thanks. I'll know it.

>  >> using alternate delimiters means you don't need to escape / which makes
>  >> it easier to read.
>
>  AM> Sure.
>
>see, you agree about easier to read. do the same with your use of {a,b}.

I can consider the opinion of most people but I won't change my own
opinion because of that. For me, {a,b} is easier. Clearer and easier.

--

Regards,
Alex.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




  1   2   3   4   5   6   7   8   9   10   >