Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > retrieving numbers.

Reply
Thread Tools

retrieving numbers.

 
 
Corne van Tonder
Guest
Posts: n/a
 
      04-11-2005
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
 
Reply With Quote
 
 
 
 
Scott Bryce
Guest
Posts: n/a
 
      04-11-2005
Corne van Tonder wrote:

> 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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-11-2005
Corne van Tonder <(E-Mail Removed)> wrote:

> 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+)/;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-12-2005
[Thank you for making me do a multi-post now, too]
[Don't do that!]
Corne van Tonder wrote:
> 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


 
Reply With Quote
 
Corne van Tonder
Guest
Posts: n/a
 
      04-12-2005
Tad McClellan wrote:

> Corne van Tonder <(E-Mail Removed)> wrote:
>
>> 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+)/;
>
>

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!
 
Reply With Quote
 
Corne van Tonder
Guest
Posts: n/a
 
      04-12-2005
Scott Bryce wrote:

> Corne van Tonder wrote:
>
>> 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


Hey Scott,

Many thanks for the reply! It worked perfectly. I now have a temporary
replacement for my pc remote ! with the help of perl, Qt and this
newsgroup!

Now, with my second bit of perl scripting under the belt, I look forward to
number 3.......THE WORLD!!!!!!.......(followed by echoing evil laugh !

Again, many thanks!
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      04-12-2005
Corne van Tonder wrote:

> 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?"
 
Reply With Quote
 
Corne van Tonder
Guest
Posts: n/a
 
      04-12-2005
Jürgen Exner wrote:

> 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
 
Reply With Quote
 
Corne van Tonder
Guest
Posts: n/a
 
      04-12-2005
Tad McClellan wrote:

> Corne van Tonder <(E-Mail Removed)> wrote:
>
>> 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+)/;
>
>


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

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

aaaaahhhhhhhh!!!!!!
 
Reply With Quote
 
Corne van Tonder
Guest
Posts: n/a
 
      04-12-2005
Chris Mattern wrote:

> Corne van Tonder wrote:
>
>> 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 ****ed about trying to get any of my re's
to work?!

This will be another entry in my perl scrapbook!

Many thanks!

>
>>
>> Many thanks for any replies.
>>
>> cvt

>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thunderbird stops retrieving messages relaxing cardshark Firefox 3 06-08-2005 11:12 PM
retrieving number from a string Corne van Tonder Perl 3 04-12-2005 07:16 AM
Server disconnect after retrieving email gwtx2 Firefox 4 12-18-2004 11:20 PM
retrieving data from array changes it??? thoin Perl 3 03-02-2004 11:37 PM
VB.NET Retrieving Identity form MSSQL2000 without using stored procedures Taras ASP .Net 2 10-05-2003 05:35 AM



Advertisments