retrieving numbers.

C

Corne van Tonder

Hey guys,

I posted this message on comp.lang.perl, but I'm not sure it acually
arrived, so I thought I'd re-post it here.....

Hey folks,

Here's something I'm struggling with at the moment that I was hoping you
guys and/or girls could help me with.

If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from the
string. This would be easy, if i was using substr() and I was sure that I
was working with only a fixed length on the numeric data, but as you can
see, it could be either 1, 2 or more chars in length. How can I do it?

Many thanks for any replies.

cvt
 
S

Scott Bryce

Corne said:
If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from the
string. This would be easy, if i was using substr() and I was sure that I
was working with only a fixed length on the numeric data, but as you can
see, it could be either 1, 2 or more chars in length. How can I do it?

http://www.perldoc.com/perl5.8.4/pod/func/split.html
 
T

Tad McClellan

Corne van Tonder said:
I posted this message on comp.lang.perl,


That newsgroup was removed many years ago.

If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from the
string.


Assuming that the string is in $_ :

my( $num ) = /^(\d+)/;
 
J

Jürgen Exner

[Thank you for making me do a multi-post now, too]
[Don't do that!]
Corne said:
I posted this message on comp.lang.perl, but I'm not sure it acually
arrived, so I thought I'd re-post it here.....

If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from
the string. This would be easy, if i was using substr() and I was
sure that I was working with only a fixed length on the numeric data,
but as you can see, it could be either 1, 2 or more chars in length.

The most trivial approach is probably: just use it.

my $s = "10 /this/is/a/file/or/directory/path";
print $s + 5;

Note: this will generate a warning under "use warnings;"; so you may want to
disable warnings for this statement.

jue
 
C

Corne van Tonder

Tad said:
That newsgroup was removed many years ago.




Assuming that the string is in $_ :

my( $num ) = /^(\d+)/;
Many thanks for the replay :) !

This is one I'm adding to my new created perl snippets file!
That said, being slow and lazy, I'm going to go with Scott's reply.

Again, many thanks!
 
C

Chris Mattern

Corne said:
Hey guys,

I posted this message on comp.lang.perl,

There is no such newsgroup, it having been split many years ago.
but I'm not sure it acually
arrived, so I thought I'd re-post it here.....

Hey folks,

Here's something I'm struggling with at the moment that I was hoping you
guys and/or girls could help me with.

If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from the
string. This would be easy, if i was using substr() and I was sure that I
was working with only a fixed length on the numeric data, but as you can
see, it could be either 1, 2 or more chars in length. How can I do it?

This looks like a job for a regular expression, not substr. The first rule
of regular expressions is Know Your Data. Can you guarantee that the string
will always start with a number that is followed by a space? It looks like
you're parsing du output, in which case, yes, you can so guarantee. In such
a case, it is easy:

if ($input_string =~ /^([0-9]*) /) {
$my_number = $1;
}
Many thanks for any replies.

cvt

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
C

Corne van Tonder

Jürgen Exner said:
my $s = "10 /this/is/a/file/or/directory/path";
print $s + 5;

Hey Jürgen,

Many thanks for the reply, and so sorry about the cross posting!!!!
print $s + 5;
I have no idea why it works, but if I say
print $s + 0;
it prints 10!
I assume this is one of those abstract peculiarities one finds with perl.

Again, many thanks for the reply, and I'll be sure to ignore
comp.lang.perl ;-)
 
C

Corne van Tonder

Tad said:
That newsgroup was removed many years ago.




Assuming that the string is in $_ :

my( $num ) = /^(\d+)/;

Oh, c$%^*t !!!!!!

That should be "reply" along with "newly created" !!!!!!

aaaaahhhhhhhh!!!!!!
 
C

Corne van Tonder

Chris said:
Corne said:
Hey guys,

I posted this message on comp.lang.perl,

There is no such newsgroup, it having been split many years ago.
but I'm not sure it acually
arrived, so I thought I'd re-post it here.....

Hey folks,

Here's something I'm struggling with at the moment that I was hoping you
guys and/or girls could help me with.

If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from the
string. This would be easy, if i was using substr() and I was sure that I
was working with only a fixed length on the numeric data, but as you can
see, it could be either 1, 2 or more chars in length. How can I do it?

This looks like a job for a regular expression, not substr. The first
rule
of regular expressions is Know Your Data. Can you guarantee that the
string
will always start with a number that is followed by a space? It looks
like you're parsing du output, in which case, yes, you can so guarantee.
In such a case, it is easy:

if ($input_string =~ /^([0-9]*) /) {
$my_number = $1;
}

Man! Have you any idea how long I pissed about trying to get any of my re's
to work?!

This will be another entry in my perl scrapbook! ;-)

Many thanks!
 
J

John Bokma

Corne said:
Man! Have you any idea how long I pissed about trying to get any of my
re's to work?!

This will be another entry in my perl scrapbook! ;-)

Read a good book on Perl first before you program. You can't learn a
language from downloading or asking code snippets.
 
J

Jürgen Exner

Corne said:
Many thanks for the reply, and so sorry about the cross posting!!!!

Actually you did not cross-post (that would have been ok, at least more or
less), but you multi-posted.
And that is severely frowned upon. Of course, this has nothing to do with
Perl.
I have no idea why it works, but if I say
print $s + 0;
it prints 10!
I assume this is one of those abstract peculiarities one finds with
perl.

Explanation is simple (and should be included in any decend Perl programming
book):
If you are applying a numerical operators on some value then Perl will use
the numerical value of that argument.
And the numerical value of a "string" is the value of the leading number of
that string or 0 in case there is no leading number.

Please note that strictly speaking there are no "strings" in Perl but only
scalars. The way you use a scalar defines if the numerical, the textual, or
the boolean value of the scalar will be used.

jue
 
J

Jürgen Exner

Corne said:
I thought while i'm at it, i may as well show you guys your
contribution to my quick and dirty remote replacement. be kind!

Which devil has bitten you to post a 428K binary in a discussion NG?
Are you nuts?

jue
 
C

Corne van Tonder

A. Sinan Unur said:
What is wrong with you?

Calm down.

Don't post binaries.

Sinan

I do apologize.

You may remember some of your best programming experiences as being your
first programming successes. Remember that feeling you got when the
computer actually did what you wanted it do? Enthusiasm is what they call
it. In my case, emotions tend to be a little exaggerated, so you were met
by a healthy handfull of over-enthusiasm.

Now, my little quick and dirty app may not be much, but damn I love that
feeling!!!

My apologies again, and many thanks for the constructive criticism :)
 
C

Corne van Tonder

Jürgen Exner said:
Actually you did not cross-post (that would have been ok, at least more or
less), but you multi-posted.
And that is severely frowned upon. Of course, this has nothing to do with
Perl.


Explanation is simple (and should be included in any decend Perl
programming book):
If you are applying a numerical operators on some value then Perl will use
the numerical value of that argument.
And the numerical value of a "string" is the value of the leading number
of that string or 0 in case there is no leading number.

Please note that strictly speaking there are no "strings" in Perl but only
scalars. The way you use a scalar defines if the numerical, the textual,
or the boolean value of the scalar will be used.

jue

Hey Jürgen,

Many thanks for that!
Your explanation is perfect and also hammers home the fact, as you guys have
pointed out, that I really should invest in a good book.

My apologies to all, hopefully for the last time.

Again, many thanks!
cvt
 
A

A. Sinan Unur

I do apologize.
....

In my case, emotions tend to be a little exaggerated, so
you were met by a healthy handfull of over-enthusiasm.

Now, my little quick and dirty app may not be much, but damn I love
that feeling!!!

My apologies again, and many thanks for the constructive criticism :)

Even forgetting about basic netiquette, I can think of no excuse for
posting a picture of your success rather than the code. The code might
help others in the future. The picture will not.

So, by all means, when you have solved your problem, post the solution
so that it can be useful to generations of readers of this newsgroup.

Sinan
 
C

Corne van Tonder

Corne said:
Hey guys,

I posted this message on comp.lang.perl, but I'm not sure it acually
arrived, so I thought I'd re-post it here.....

Hey folks,

Here's something I'm struggling with at the moment that I was hoping you
guys and/or girls could help me with.

If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from the
string. This would be easy, if i was using substr() and I was sure that I
was working with only a fixed length on the numeric data, but as you can
see, it could be either 1, 2 or more chars in length. How can I do it?

Many thanks for any replies.

cvt

Here then is the accumulation of this discussion and the implementation of
the final solution.
I needed to populate a QListview object with the contents of my main
playlist which is stored in a text file with the above formating. The
script opens the playlist, splits the each entry into two variables which
is then used for my two columns of data in my QListView object.

Ready?.......

while(<DB>) {
($ent_id, $ent_path) = split();
Qt::ListViewItem( $frm->lvw_list, $ent_id, $ent_path);
}

Things to note.....
1) By default split() uses $_
2) The resulting QListViewItem object created from the call to the
QListViewItem constructor is automatically appended to the registered
parent object, which was passed as the first argument of the contructor.
Be sure to pass a valid parent.
3) Any correction will be posted by those reading this.

Resources.....
1) http://www.perldoc.com/perl5.8.4/pod/func/split.html
2) The 'Qt Assistant' as distributed with most Linux distributions.
3) This newsgroup (comp.lang.perl.misc)
 
T

Tad McClellan

Corne van Tonder said:
($ent_id, $ent_path) = split();
Things to note.....
1) By default split() uses $_


No programmer needs to make note of that, because they would
have read the documentation for the function they are using,
which says pretty clearly what split defaults to using.
 
G

Gunnar Hjalmarsson

Tad said:
No programmer needs to make note of that, because they would
have read the documentation for the function they are using,
which says pretty clearly what split defaults to using.

Please, Tad...

To Corne: I for one think you made a summary of this thread worthy of
imitation. Thanks!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top