a length of variable question

J

justme

hi

in my script i will process from a database and assign a value to a
variable $var1
The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
"string" format. There will be spaces in front as well as behind it.

eg

$var1 = xxxxxxx2xxx
or
$var1 = xxx3xxxxxxxxxxx

where x = spaces.

I want to remove these spaces and just get the number. so i tried to
use

$var1 =~ s/(.*)(\d)(.*)/\2/ ;

Then when i print the length of $var1, it says length is 2. Is there
anything wrong with my regexp?
thanks for help
 
T

Tony Curtis

On 21 Nov 2004 18:59:30 -0800,
hi in my script i will process from a database and assign a
value to a variable $var1 The value assigned to $var1 is a
number. eg 1, 2 or 3. But it is in "string" format. There
will be spaces in front as well as behind it.

would just casting it into an int work?

$m = ' 4 ';
$n = int $m;
print "$n\n";

4
 
A

A. Sinan Unur

(e-mail address removed) (justme) wrote in
in my script i will process from a database and assign a value to a
variable $var1
The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
"string" format. There will be spaces in front as well as behind it.

eg

$var1 = xxxxxxx2xxx
or
$var1 = xxx3xxxxxxxxxxx

where x = spaces.

I want to remove these spaces and just get the number. so i tried to
use

$var1 =~ s/(.*)(\d)(.*)/\2/ ;

Please read the posting guidelines posted here frequently.

You seem not to be using strict or warnings.

C:\Home> cat t6.pl
use strict;
use warnings;

my $m = ' 4 ';
$m =~ s/(.*)(\d)(.*)/\2/ ;
print $m, "\n", length $m, "\n";

C:\Home> t6
\2 better written as $2 at C:\Home\asu1\t6.pl line 5.
4
1
Then when i print the length of $var1, it says length is 2. Is there
anything wrong with my regexp?

If you are not going to use the leading and trailing whitespace, there is
no need to capture it.

C:\Home> cat t6.pl
use strict;
use warnings;

my $m = ' 4 ';
$m =~ s/\s*(\d)\s*/$1/ ;
print $m, "\n", length $m, "\n";

C:\Home> t6
4
1

But then, if all you want is to remove space characters, there is no need
to use s/// anyway:

C:\Home> cat t6.pl
use strict;
use warnings;

my $m = ' 4 ';
$m =~ tr/ //d;
print $m, "\n", length $m, "\n";

C:\Home> t6
4
1
 
G

Gunnar Hjalmarsson

justme said:
in my script i will process from a database and assign a value to a
variable $var1
The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
"string" format. There will be spaces in front as well as behind it.

eg

$var1 = xxxxxxx2xxx
or
$var1 = xxx3xxxxxxxxxxx

where x = spaces.

I want to remove these spaces and just get the number. so i tried to
use

$var1 =~ s/(.*)(\d)(.*)/\2/ ;

Then when i print the length of $var1, it says length is 2. Is there
anything wrong with my regexp?

1) The capturing parenteses around '.*' are redundant.

2) If the length of $var1 is 2 after that operation, the string contains
anything else but a digit, probably a newline. Is that what you want?

3) The regex does not cover the possibility that the number consists of
more than one digit. Is that intentional?

4) If you enable warnings (why didn't you do so before posting?), Perl
will tell you about another thing that isn't good.
 
J

Jürgen Exner

justme said:
The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
"string" format. There will be spaces in front as well as behind it.

eg

$var1 = xxxxxxx2xxx
or
$var1 = xxx3xxxxxxxxxxx

where x = spaces.

I want to remove these spaces and just get the number. so i tried to
use

$var1 =~ s/(.*)(\d)(.*)/\2/ ;

Way, way to complicated.
As I wrote just yesterday in a different thread to cast a string to a
numerical value you simply add 0.

my $var1 = ' 3 ';
print $var1 + 0;

This will work as long as there are just blanks before the number.

jue
 
J

James Willmore

would just casting it into an int work?

$m = ' 4 ';
$n = int $m;
print "$n\n";

4

It would ... if Perl were another language :)

scalars (very simply those variables represented with a '$' in front of
the variable name) are type independent in Perl. Other languages (like
C++ and Java) *required* you to associate a variable with a type - Perl
doesn't.

`perldoc -q scalar` might help, as well as `perldoc perldata`

HTH

Jim
 
P

Paul Lalli

James Willmore said:
It would ... if Perl were another language :)

This language will do just fine.
scalars (very simply those variables represented with a '$' in front of
the variable name) are type independent in Perl. Other languages (like
C++ and Java) *required* you to associate a variable with a type - Perl
doesn't.

All of which is wholly irrelevant to the discussion at hand.

Perl doesn't have strict 'types', that's true. But it does have
contexts. Specifically, it has both a string and an numeric context.
When a variable containing a string is used in a numeric context - such
as that provided by the int() function - perl scans the variable
character by character to determine what number the string represents.
Leading whitespace is ignored in this scan.
`perldoc -q scalar` might help, as well as `perldoc perldata`

perldoc -f int might help you, as would simply running the example
program above.

Hope that clarifies,
Paul Lalli
 
T

Tad McClellan

James Willmore said:
It would ...


It does!

if Perl were another language :)


Tony's code looked just like Perl to me. :)

scalars (very simply those variables represented with a '$' in front of
the variable name) are type independent in Perl.


Right, but what was wanted by the OP was to remove leading and trailing
whitespace, and Tony's code does just that (but it is not "casting"
at all).

It works because of the rules Perl uses when converting a
string into a number:

1) ignore leading whitespace

2) ignore anything after the 1st char that cannot be part of a number.

Rule 1 removes the leading, Rule 2 removes the trailing.

Of course, the $n temporary variable is not needed:

$m = int $m;
or
$m = 0 + $m;
 
T

Tassilo v. Parseval

Also sprach Tad McClellan:
It works because of the rules Perl uses when converting a
string into a number:

1) ignore leading whitespace

2) ignore anything after the 1st char that cannot be part of a number.

Rule 1 removes the leading, Rule 2 removes the trailing.

Of course, the $n temporary variable is not needed:

$m = int $m;
or
$m = 0 + $m;

And yet, I'd drop the first of the anove two alternatives. int() is
really used for dropping the decimal part of a floating point number.
It's not its primary purpose to convert a string to a number as it will
only convert it to an integer. This starts to matter when $m is
something like

$m = ' 1.5 ';

Tassilo
 
T

Tony Curtis

Right, but what was wanted by the OP was to remove leading
and trailing whitespace, and Tony's code does just that (but
it is not "casting" at all).

Yeah, years of C wrecks yer brain :)
Of course, the $n temporary variable is not needed:
$m = int $m; or $m = 0 + $m;

I usually include more intermediate variables in examples than
I might ordinarily use in "real" code.

t
 
J

justme

hi

in my script i will process from a database and assign a value to a
variable $var1
The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
"string" format. There will be spaces in front as well as behind it.

eg

$var1 = xxxxxxx2xxx
or
$var1 = xxx3xxxxxxxxxxx

where x = spaces.

I want to remove these spaces and just get the number. so i tried to
use

$var1 =~ s/(.*)(\d)(.*)/\2/ ;

Then when i print the length of $var1, it says length is 2. Is there
anything wrong with my regexp?
thanks for help

thanks guys for your help.
my original intention was to get rid of everything before the digit
and after.
I do not know what other embedded characters would be infront of the
digit (which i can't 'see') besides the spaces and newlines, so my
intention was to get rid of everything before the digit as well as
after it. that's why i use (.*). Since its redundant as suggested in
some posts...i would refine the expression. Thanks.
Anyway, i found the problem, its a newline. But why doesn't (.*) get
rid of it?
hmm... I did not use warnings as my perl version is very old. :)
Thanks again for the help
 
J

Jürgen Exner

justme wrote:
[RE question]
Anyway, i found the problem, its a newline. But why doesn't (.*) get
rid of it?

Which part of "perldoc perlre" don't you understand?

<quote>
In particular the following metacharacters have their standard
*egrep*-ish meanings:
[...]
. Match any character (except newline)
hmm... I did not use warnings as my perl version is very old. :)

Maybe time to upgrade?

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top