Home > Archive > PERL Beginners > January 2006 > getting a number out of a string....
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
getting a number out of a string....
|
|
| David Gilden 2006-01-10, 4:01 am |
| Greetings,
How does one just get the number part out of a string?
The script below just prints 1.
#!/usr/bin/perl=20
@str =3D ('Gambia001.tiff','Gambia0021.tiff','Gambia031.tiff','Gambia035.ti=
ff') ;
foreach $str (@str) {
$num =3D ($str =3D~ /\d+/);
print "$str : $num\n";
}
Thanks!
Dave Gilden
(kora musician / audiophile / webmaster @ www.coraconnection.com / Ft. Wor=
th, TX, USA)
Endorsing Artist for the Moog Music:
<http://www.moogmusic.com/artists.html?cat_id=3D25>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D
Cora Connection: Your West African Music Source
Resources, Recordings, Instruments & More!
<http://www.coraconnection.com/>=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D
| |
| Paul Lalli 2006-01-10, 4:01 am |
|
David Gilden wrote:
> Greetings,
>
> How does one just get the number part out of a string?
>
> The script below just prints 1.
>
> #!/usr/bin/perl
>
> @str = ('Gambia001.tiff','Gambia0021.tiff','Gambia031.tiff','Gambia035.tiff') ;
>
> foreach $str (@str) {
> $num = ($str =~ /\d+/);
You are running a pattern match in a scalar context. In a scalar
context, the mattern match simply returns 1 or "" (true or false)
depending on whether or not the pattern match succeeded.
You have several options:
(1)Capture what you want to match, and refer to it later:
if ($str =~ /(\d+)/){
$num = $1;
# . . .
}
(2) Call the pattern match in list context, matching globally:
($num) = ($str =~ /\d+/g);
(3) Call the pattern match in list context, and capture what you want
to match:
($num) = ($str =~ /(\d+)/);
This is all explained in detail in:
perldoc perlop
perldoc perlretut
perldoc perlre
perldoc perlreref
Paul Lalli
| |
| Chris Devers 2006-01-10, 4:01 am |
| On Wed, 28 Dec 2005, David Gilden wrote:
> How does one just get the number part out of a string?
>
> The script below just prints 1.
Right. All it's doing is reporting a successful, true, match.
You need to fix where the parentheses are being used:
> foreach $str (@str) {
> $num = ($str =~ /\d+/);
> print "$str : $num\n";
> }
foreach (@str) {
( $num = $_ ) =~ /\d+/;
print "$_ : $num\n";
}
That should work better, and also avoids the confusing and unnecessary
$str temporary scalar that looks too much like the $str[] array. Also,
it's usefully indented, because Readability Matters.
--
Chris Devers
DO NOT LEAVE IT IS NOT REAL
| |
| Todd W 2006-01-10, 4:01 am |
|
"David Gilden" <dowda@coraconnection.com> wrote in message
news:r02010500-1039-0B6C98E5780611DA9ADB0003935B6868@[192.168.0.100]...
> Greetings,
>
> How does one just get the number part out of a string?
>
> The script below just prints 1.
>
> #!/usr/bin/perl
>
> @str =
('Gambia001.tiff','Gambia0021.tiff','Gambia031.tiff','Gambia035.tiff') ;
>
> foreach $str (@str) {
> $num = ($str =~ /\d+/);
> print "$str : $num\n";
> }
You migght try this:
use warnings;
use strict;
my @str = qw(Gambia001.tiff Gambia0021.tiff Gambia031.tiff Gambia035.tiff);
foreach my $str (@str) {
my($num) = $str =~ /(\d+)/;
print "$str : $num\n";
}
output:
$ perl extrt.pm
Gambia001.tiff : 001
Gambia0021.tiff : 0021
Gambia031.tiff : 031
Gambia035.tiff : 035
> Thanks!
You bet!
Todd W.
| |
| Paul Lalli 2006-01-10, 4:01 am |
|
Chris Devers wrote:
> On Wed, 28 Dec 2005, David Gilden wrote:
>
>
> Right. All it's doing is reporting a successful, true, match.
>
> You need to fix where the parentheses are being used:
>
>
> foreach (@str) {
> ( $num = $_ ) =~ /\d+/;
> print "$_ : $num\n";
> }
>
> That should work better, and also avoids the confusing and unnecessary
> $str temporary scalar that looks too much like the $str[] array. Also,
> it's usefully indented, because Readability Matters.
>
Please run your code before posting it as a solution.
This does absolutely nothing the OP asked for. This assigns $num to be
the value of $_, and then runs a pattern match against that value. No
where is the \d+ parsed or stored. This code prints two copies of the
same value, separated by a space-colon-space.
Paul Lalli
| |
| David Gilden 2006-01-10, 4:01 am |
| Chris,
Thanks and Happy New Year.
Dave
(kora musician / audiophile / webmaster @ www.coraconnection.com / Ft. Wor=
th, TX, USA)
>=20
> Right. All it's doing is reporting a successful, true, match.
>=20
> You need to fix where the parentheses are being used:
> =20
>=20
> foreach (@str) {
> ( $num =3D $_ ) =3D~ /\d+/;
> print "$_ : $num\n";
> }
>=20
> That should work better, and also avoids the confusing and unnecessary=20
> $str temporary scalar that looks too much like the $str[] array. Also,=20
> it's usefully indented, because Readability Matters.
>=20
>=20
|
|
|
|
|