help I'm a beginner
Author Message
help I'm a beginner

I know to most of you this will be kind of easy, but I was wondering if
there was a way to count characters from a line.  It doesn't really
mention any of that in the book I have so I was wondering if you guys
can help me out.  For instance if this was a line :

Hello I am going insane.\n

I can get the number of caharacters to be 26 and store that in a
variable.

This is my 2nd week with the language and I was hoping you guys know

Fri, 22 Dec 2000 03:00:00 GMT
help I'm a beginner

Quote:
> I know to most of you this will be kind of easy, but I was wondering if
> there was a way to count characters from a line.  It doesn't really
> mention any of that in the book I have so I was wondering if you guys
> can help me out.  For instance if this was a line :

> Hello I am going insane.\n

> I can get the number of caharacters to be 26 and store that in a
> variable.

The function is length().  You can learn about it by executing the
command

perldoc -f length

If your book is 'Learning Perl' you will find 'length()' in the index,
pointing to examples, though it isn't discussed formally in the text
proper.

--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/

Fri, 22 Dec 2000 03:00:00 GMT
help I'm a beginner

Ya know, something funny about length.. you might think that it would treat
8.10 as a string and return 4... but in fact it returns 3, and this only
seems to happen if you use length on a number where the last digit is 0.

A better way is:

\$num = 8.10;
\$num =~ tr/0-9A-Za-z/0-9A-Za-z/r; # (I forget what goes here, but probably
an
r)

This would return 4 rather than 3.

Erik

Quote:
>The function is length().  You can learn about it by executing the
>command

Fri, 22 Dec 2000 03:00:00 GMT
help I'm a beginner

Quote:
> Ya know, something funny about length.. you might think that it would treat
> 8.10 as a string and return 4... but in fact it returns 3, and this only
> seems to happen if you use length on a number where the last digit is 0.

> A better way is:

> \$num = 8.10;
> \$num =~ tr/0-9A-Za-z/0-9A-Za-z/r; # (I forget what goes here, but probably
> an
> r)

> This would return 4 rather than 3.

PLEASE don't post solutions that begin 'A better way is:' off the top of
code.

1. The expression you were trying to present should be simply

\$num =~ tr/whatever//

whose value is the number of matches in the set of characters between the
first two slashes.

2. You left out the '.' in your set of characters.  This just shows how
silly it is to attempt such an enumeration.

3. Even if your set were complete, the expression would still have the
value 3, because the internal storage of \$num = 8.10 is identical to 8.1
or 8.1000000.  It is the set of bits that when interpreted *as a number*
produces 8.1  .  If you want the string '8.10', then you must store it as
a string:

\$num = '8.10';

and length \$num is indeed 4.

You may be competing for the record for the highest number of errors per
line of code. :-)

--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/

Fri, 22 Dec 2000 03:00:00 GMT
help I'm a beginner

Quote:
> Ya know, something funny about length.. you might think that it would treat
> 8.10 as a string and return 4... but in fact it returns 3, and this only
> seems to happen if you use length on a number where the last digit is 0.

Give this a try:

\$num = 8.10;
print \$num, " - ", length(\$num), "\n";

\$num = "8.10";
print \$num, " - ", length(\$num), "\n";

You'll notice that in the first instance, perl gets rid of the last 0,
because it interprets \$num as a number, which is correct. If you want
it to be a string, you should make that clear. Perl does make a
distinction between numbers and strings, but it can convert silently
from one to the other if needed. This is all explained in the
documentation.

Martien
--
Martien Verbruggen                  |
Webmaster www.tradingpost.com.au    | In the fight between you and the world,
Commercial Dynamics Pty. Ltd.       | back the world - Franz Kafka
NSW, Australia                      |

Fri, 22 Dec 2000 03:00:00 GMT
help I'm a beginner

: \$num = 8.10;
: print \$num, " - ", length(\$num), "\n";

: \$num = "8.10";
: print \$num, " - ", length(\$num), "\n";

: You'll notice that in the first instance, perl gets rid of the last 0,
: because it interprets \$num as a number, which is correct. If you want
: it to be a string, you should make that clear. Perl does make a
: distinction between numbers and strings, but it can convert silently
: from one to the other if needed. This is all explained in the
: documentation.

Well, how do I convert from string to number?
I tried \$num = 0 + \$num, but it gives warning if I use -w switch
and \$num contains a string.

Ok, I know I could use regexp to extract numeric value from string, but
is there any other way to do it?

tukki:/home/sapapo/prog% cat ./strnum.pl
#!/opt/bin/perl -w

if ((~\$_ & \$_) ne '0') {
print "\$_ is string\n";
} else {
print "\$_ number\n";
}

\$_ = 0 + \$_;
if ((~\$_ & \$_) ne '0') {
print "\$_ is now string\n";
} else {
print "\$_ is now number\n";
}

Quote:
}

tukki:/home/sapapo/prog% ./strnum.pl
123 number
123 is now number
234 is string
234 is now number
cat is string
Argument "cat" isn't numeric in add at strnum.pl line 12.
0 is now number
456abc is string
Argument "456abc" isn't numeric in add at strnum.pl line 12.
456 is now number

--
Sami Poikonen

Sat, 23 Dec 2000 03:00:00 GMT
help I'm a beginner

Quote:
>Well, how do I convert from string to number?

Wrong question.  You don't convert string to number in Perl.
If you use it in a numeric context, it's used like a number.

Quote:
>I tried \$num = 0 + \$num, but it gives warning if I use -w switch
>and \$num contains a string.

``Hi.  How do I drive my car over a bridge?  I tried doing it the
usual way, but there is a sign that says MAXIMUM BRIDGE LOAD: 28 TONS.''

The warning is advising you that you might be doing something wrong.
In this case, you know you're not doing anything wrong, so you should
ignore the warning.

Sat, 23 Dec 2000 03:00:00 GMT
help I'm a beginner
[This followup was posted to comp.lang.perl.misc and a copy was sent to
the cited author.]

Quote:

...
> >I tried \$num = 0 + \$num, but it gives warning if I use -w switch
> >and \$num contains a string.

> ``Hi.  How do I drive my car over a bridge?  I tried doing it the
> usual way, but there is a sign that says MAXIMUM BRIDGE LOAD: 28 TONS.''

> The warning is advising you that you might be doing something wrong.
> In this case, you know you're not doing anything wrong, so you should
> ignore the warning.

In the version of perl I use (5.004_03), the warning indicates that the
conversion of \$num from string to numeric was terminated by a character
in the string that is neither part of a valid number nor white-space.  It
probably should not be ignored.

If the submitter really wants to 'ignore' (suppress) the warning, he
might try:

{ local \$^W; \$num += 0 }

--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/

Sat, 23 Dec 2000 03:00:00 GMT
help I'm a beginner

Quote:
> Well, how do I convert from string to number?
> I tried \$num = 0 + \$num, but it gives warning if I use -w switch
> and \$num contains a string.

It gives a warning if the string contains something that doesn't look
like a number. If the string contains something that looks like a
number, it gets silently converted. You should have looked at the
_whole_ warning, which would be something like:

Argument "aa" isn't numeric in add at ./tt.pl line 13.

How do you expect anything to convert some random string to a number?

Now _what_ exactly do you want to convert? Maybe you should see if
your string looks like a number first. See perlfaq4 as well:

# perldoc perlfaq4
/How do I determine whether a scalar is a  number/whole/integer/float?

Quote:
> Ok, I know I could use regexp to extract numeric value from string, but
> is there any other way to do it?

Ok. This is something completely different. You don't want to
_convert_ a scalar to a number, but you want to _extract_ a number
from a string. A regexp probably would be the best way of doing this.
If the number is always at the start of your string, you just use it
in a numeric context, and ignore the warnings, but I wouldn't
recommend that.

The fact that perl warns you that '456aa' is not a number is a good
thing, because it simply isn't a number.

Martien
--
Martien Verbruggen                  |
Webmaster www.tradingpost.com.au    | That's not a lie, it's a
Commercial Dynamics Pty. Ltd.       | terminological inexactitude.
NSW, Australia                      |

Sat, 23 Dec 2000 03:00:00 GMT
help I'm a beginner

Quote:
>The fact that perl warns you that '456aa' is not a number is a good
>thing, because it simply isn't a number.

well, not a decimal number anyway.  maybe it's just the particular
example, but hex() sprang to mind when i saw '456aa' next to 'number'.

:)

--

CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
Perl Mongers Travel Deals! <URL:http://www.pm.org/travel.html>

Sat, 23 Dec 2000 03:00:00 GMT
help I'm a beginner

Quote:

>>The fact that perl warns you that '456aa' is not a number is a good
>>thing, because it simply isn't a number.

> well, not a decimal number anyway.  maybe it's just the particular
> example, but hex() sprang to mind when i saw '456aa' next to 'number'.

heh. ok, ok. Maybe I should have used something else, but then, It's
always possible to come up with a base that includes any characters
that I can come up with :)

Martien
--
Martien Verbruggen                  |