reference noob

The program:=0A--=0A#use strict;=0Ause warnings;=0A=0Amy $field =3D shift [at] =
ARGV;=0Amy $regex =3D '(\w+)\s*' x $field;=0A=0Awhile (<STDIN>) {=0A=09if (=
/$regex/) {=0A=09=09print "$$field\n";=09# refers to a match variable=0A=09=
}=0A}=0A--=0A=0AExample Usage: =0A--=0A$ echo 'Strange New World!' | ./this=
_program 3=0A$ World=0A--=0A=0AHow could I do this with 'use strict' turned=
on? =0A=0A=0A

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
precheca123 [ Do, 03 Februar 2011 14:05 ] [ ID #2054536 ]

Re: reference noob

On 11-02-03 08:05 AM, Téssio Fechine wrote:
> The program:
> --
> #use strict;
> use warnings;
>
> my $field = shift [at] ARGV;
> my $regex = '(\w+)\s*' x $field;
>
> while (<STDIN>) {
> if (/$regex/) {
> print "$$field\n"; # refers to a match variable
> }
> }
> --
>
> Example Usage:
> --
> $ echo 'Strange New World!' | ./this_program 3
> $ World
> --
>
> How could I do this with 'use strict' turned on?

#!/usr/bin/perl

use strict;
use warnings;

my $field = shift [at] ARGV;
my $regex = '(\w+)\s*' x $field;

while (<STDIN>) {
if ( my [at] capture = /$regex/) {
print "$capture[$field-1]\n";
}
}




--
Just my 0.00000002 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.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Do, 03 Februar 2011 21:11 ] [ ID #2054537 ]

Re: reference noob

>>>>> "TF" =3D=3D T=E9ssio Fechine <precheca123 [at] yahoo.com.br> writes:

TF> The program:
TF> --
TF> #use strict;
TF> use warnings;

TF> my $field =3D shift [at] ARGV;
TF> my $regex =3D '(\w+)\s*' x $field;

TF> while (<STDIN>) {
TF> if (/$regex/) {
TF> print "$$field\n"; # refers to a match variable
TF> }
TF> }
TF> --

TF> Example Usage:
TF> --
TF> $ echo 'Strange New World!' | ./this_program 3
TF> $ World
TF> --

TF> How could I do this with 'use strict' turned on?

you don't. it is nasty coding and will bite you for sure down the road.

it is trivial to deal with in other ways. the simplest is to just assign
the regex results in list context. that will return a list of all the
grabs and you can loop over that.

uri

--
Uri Guttman ------ uri [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Do, 03 Februar 2011 21:11 ] [ ID #2054538 ]

Re: reference noob

--001636c5a5d02e2792049b6667bc
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

print "$field\n"

~Parag



2011/2/3 T=C3=A9ssio Fechine <precheca123 [at] yahoo.com.br>

> The program:
> --
> #use strict;
> use warnings;
>
> my $field =3D shift [at] ARGV;
> my $regex =3D '(\w+)\s*' x $field;
>
> while (<STDIN>) {
> if (/$regex/) {
> print "$$field\n"; # refers to a match variable
> }
> }
> --
>
> Example Usage:
> --
> $ echo 'Strange New World!' | ./this_program 3
> $ World
> --
>
> How could I do this with 'use strict' turned on?
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>

--001636c5a5d02e2792049b6667bc--
Parag Kalra [ Do, 03 Februar 2011 21:13 ] [ ID #2054539 ]

Re: reference noob

On 2/3/11 Thu Feb 3, 2011 5:05 AM, "T=E9ssio Fechine"
<precheca123 [at] yahoo.com.br> scribbled:

> The program:
> --
> #use strict;
> use warnings;
>
> my $field =3D shift [at] ARGV;
> my $regex =3D '(\w+)\s*' x $field;
>
> while (<STDIN>) {
> if (/$regex/) {
> print "$$field\n"; # refers to a match variable
> }
> }
> --
>
> Example Usage:
> --
> $ echo 'Strange New World!' | ./this_program 3
> $ World
> --
>
> How could I do this with 'use strict' turned on?

Use the [at] - and [at] + arrays, which contain the beginning and ending offsets of
the captured substrings.

See 'perldoc perlvar'.



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Do, 03 Februar 2011 21:21 ] [ ID #2054540 ]

Re: reference noob

>>>>> "PK" == Parag Kalra <paragkalra [at] gmail.com> writes:

PK> print "$field\n"

what would that supposedly do to help with the question?

and please bottom post. this is a good example. your one line of code
should be BELOW the code it replaces or purports to fix.

thanx,

uri

--
Uri Guttman ------ uri [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Do, 03 Februar 2011 21:23 ] [ ID #2054541 ]
Perl » gmane.comp.lang.perl.beginners » reference noob

Vorheriges Thema: manipulating configuration files package, object oriented? moose?
Nächstes Thema: regular expression for special html characters