retrieving numbers.

Discussion in 'Perl Misc' started by Corne van Tonder, Apr 11, 2005.

  1. 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
     
    Corne van Tonder, Apr 11, 2005
    #1
    1. Advertising

  2. Corne van Tonder

    Scott Bryce Guest

    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
     
    Scott Bryce, Apr 11, 2005
    #2
    1. Advertising

  3. Corne van Tonder <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 12, 2005
    #3
  4. [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
     
    Jürgen Exner, Apr 12, 2005
    #4
  5. Tad McClellan wrote:

    > Corne van Tonder <> 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!
     
    Corne van Tonder, Apr 12, 2005
    #5
  6. 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!
     
    Corne van Tonder, Apr 12, 2005
    #6
  7. 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?"
     
    Chris Mattern, Apr 12, 2005
    #7
  8. 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 ;-)
     
    Corne van Tonder, Apr 12, 2005
    #8
  9. Tad McClellan wrote:

    > Corne van Tonder <> 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!!!!!!
     
    Corne van Tonder, Apr 12, 2005
    #9
  10. 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 pissed 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

    >
     
    Corne van Tonder, Apr 12, 2005
    #10
  11. Corne van Tonder

    John Bokma Guest

    Corne van Tonder wrote:

    > 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.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Apr 12, 2005
    #11
  12. Corne van Tonder <> wrote in news:eek:4G6e.78592
    $:

    > Attachment decoded: snapshot2.png


    What is wrong with you?

    Calm down.

    Don't post binaries.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 12, 2005
    #12
  13. Corne van Tonder wrote:
    > Jürgen Exner wrote:
    >
    >> my $s = "10 /this/is/a/file/or/directory/path";
    >> print $s + 5;

    >
    > 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.

    >> 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.


    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ürgen Exner, Apr 12, 2005
    #13
  14. Corne van Tonder wrote:
    > 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
     
    Jürgen Exner, Apr 12, 2005
    #14
  15. A. Sinan Unur wrote:

    > Corne van Tonder <> wrote in news:eek:4G6e.78592
    > $:
    >
    >> Attachment decoded: snapshot2.png

    >
    > 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 :)
     
    Corne van Tonder, Apr 12, 2005
    #15
  16. Jürgen Exner wrote:

    > Corne van Tonder wrote:
    >> Jürgen Exner wrote:
    >>
    >>> my $s = "10 /this/is/a/file/or/directory/path";
    >>> print $s + 5;

    >>
    >> 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.
    >
    >>> 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.

    >
    > 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
     
    Corne van Tonder, Apr 12, 2005
    #16
  17. Corne van Tonder <> wrote in
    news:%CG6e.54241$:

    > A. Sinan Unur wrote:
    >
    >> Corne van Tonder <> wrote in news:eek:4G6e.78592
    >> $:
    >>
    >>> Attachment decoded: snapshot2.png

    >>
    >> What is wrong with you?
    >>
    >> Calm down.
    >>
    >> Don't post binaries.
    >>
    >> Sinan

    >
    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 12, 2005
    #17
  18. The Final Solution Then.

    Corne van Tonder wrote:

    > 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)
     
    Corne van Tonder, Apr 12, 2005
    #18
  19. Re: The Final Solution Then.

    Corne van Tonder <> wrote:

    > ($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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 12, 2005
    #19
  20. Re: The Final Solution Then.

    Tad McClellan wrote:
    > Corne van Tonder <> wrote:
    >>
    >> ($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.


    Please, Tad...

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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 12, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesse
    Replies:
    2
    Views:
    350
    Jeff Schwab
    Dec 13, 2003
  2. Subra
    Replies:
    25
    Views:
    1,195
    user923005
    Mar 8, 2007
  3. Andrew Tatum

    Fibonacci Numbers and Lucas Numbers

    Andrew Tatum, May 26, 2007, in forum: C++
    Replies:
    6
    Views:
    565
    Howard
    May 27, 2007
  4. Lance Hoffmeyer
    Replies:
    2
    Views:
    524
    Lance Hoffmeyer
    Jul 26, 2007
  5. Steve

    Retrieving numbers from TextRange?

    Steve, Jun 5, 2005, in forum: Javascript
    Replies:
    0
    Views:
    81
    Steve
    Jun 5, 2005
Loading...

Share This Page