Professional Web Applications Themes

string to integer - Macromedia Flash Data Integration

I have an interesting problem... Perl seems to convert hex numbers which are explicit constants in the code just fine. For example, my $val = 0xFF; is no different from my $val = 255; However, if $val is assigned from reading 0xFF from a file or user input, this natural conversion seems not to occur. $val is treated like a string instead of a number. I am required to "use strict;" and so I get warnings about this when I use $val as an argument to a third party (COTS) subroutine that expects a number instead of a string, e.g., ...

  1. #1

    Default string to integer

    I have an interesting problem...

    Perl seems to convert hex numbers which are explicit constants in the code just
    fine. For example,

    my $val = 0xFF;

    is no different from

    my $val = 255;

    However, if $val is assigned from reading 0xFF from a file or user input, this
    natural conversion seems not to occur. $val is treated like a string instead of
    a number. I am required to "use strict;" and so I get warnings about this when
    I use $val as an argument to a third party (COTS) subroutine that expects a
    number instead of a string, e.g., 'Argument "0xFF" isn't numeric in subroutine
    entry at (eval 2) line 10.'

    I tried something like this:

    int($val)

    but get a similar error:

    'Argument "0xFF" isn't numeric in int at myscript.pl line 98.'

    The bottom line is how can I convert a string into an integer or a float??

    Mark

    Mark Fenbers Guest

  2. #2

    Default Re: string to integer

    >> On Wed, 17 Sep 2003 10:06:00 -0400,
    >> Mark Fenbers <Mark.Fenbersnoaa.gov> said:
    > I have an interesting problem... Perl seems to convert
    > hex numbers which are explicit constants in the code
    > just fine. For example,
    > my $val = 0xFF;
    > is no different from
    > my $val = 255;
    > However, if $val is assigned from reading 0xFF from a
    > file or user input, this natural conversion seems not to
    > occur. $val is treated like a string instead of a
    It *is* a string, consisting of the characters

    '0' 'x' 'F' 'F'

    Actually, you'll be mortified to know you've answered
    your own question:

    perldoc -f hex

    and more generally

    perldoc -q convert

    hth
    t
    Tony Curtis Guest

  3. #3

    Default Re: string to integer

    Ah ha!!

    hex($val) instead of int($val)...

    This solves my problem partially, but I was actually hoping for a solution to
    accept either hex or decimal formats (e.g., 0xff or 255). If there is none, I
    can add a few lines of code to test for '0x' and use hex() if found...

    Mark

    Tony Curtis wrote:
    > >> On Wed, 17 Sep 2003 10:06:00 -0400,
    > >> Mark Fenbers <Mark.Fenbersnoaa.gov> said:
    >
    > > I have an interesting problem... Perl seems to convert
    > > hex numbers which are explicit constants in the code
    > > just fine. For example,
    >
    > > my $val = 0xFF;
    >
    > > is no different from
    >
    > > my $val = 255;
    >
    > > However, if $val is assigned from reading 0xFF from a
    > > file or user input, this natural conversion seems not to
    > > occur. $val is treated like a string instead of a
    >
    > It *is* a string, consisting of the characters
    >
    > '0' 'x' 'F' 'F'
    >
    > Actually, you'll be mortified to know you've answered
    > your own question:
    >
    > perldoc -f hex
    >
    > and more generally
    >
    > perldoc -q convert
    >
    > hth
    > t
    Mark Fenbers Guest

  4. #4

    Default Re: string to integer

    Mark Fenbers <Mark.Fenbersnoaa.gov> wrote:
    > This is a multi-part message in MIME format.

    Why is this is a multi-part message in MIME format?

    Usenet is a plain text medium.

    Using MIME means that less people will see your posts.

    > I have an interesting problem...

    No, you have discovered a very fundamental aspect of computer science,
    the difference between "code" and "data".

    > Perl seems to convert hex numbers which are explicit constants in the code

    Because then it is "code".

    > if $val is assigned from reading 0xFF from a file or user input,

    Because then it is "data".

    > $val is treated like a string instead of
    > a number.

    Good, since it *is* a string instead of a number.

    > I am required to "use strict;" and so I get warnings

    "use strict" does NOT issue warnings.

    "use warnings" issues warnings.

    > The bottom line is how can I convert a string into an integer or a float??

    You can't.

    You can, however, convert a hex string into the corresponding
    numerical value. The answer to that question ends up sounding
    like you are joking with us...

    perldoc -f hex

    :-)


    --
    Tad McClellan SGML consulting
    [email]tadmcaugustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  5. #5

    Default Re: string to integer

    Mark Fenbers wrote:
    >hex($val) instead of int($val)...
    >
    >This solves my problem partially, but I was actually hoping for a solution to
    >accept either hex or decimal formats (e.g., 0xff or 255). If there is none, I
    >can add a few lines of code to test for '0x' and use hex() if found...
    I'd also like to point towards oct(). By contrast, oct() is a little
    smarter, it can do not just octal, but binary and hex conversions too,
    with the proper letter flag. So my test would be:

    $int = $val =~ /^0/ ? oct($val) : 0+$val;

    You can try it with "123", "0177" (=127), "0xFF" (=255) and "0b1101"
    (=13), for example:

    foreach my $val (qw(123 0177 0xFF 0b1101)) {
    printf "'%s' is %d\n", $val, $val =~ /^0/ ? oct($val) : 0+$val;
    }

    This simple test will only work for integers, though: "0.123" isn't
    supposed to be treated as octal.

    --
    Bart.
    Bart Lateur Guest

  6. #6

    Default Re: string to integer

    I don't post very often to newsgroups because there always has to be some
    condescending know-it-all to chime in his two cents. Someone always finds a way
    to be rude... Tad, your comments were critical, not helpful. *sigh* I guess I
    expected better etiquette from a professional...

    Mark

    Tad McClellan wrote:
    > Mark Fenbers <Mark.Fenbersnoaa.gov> wrote:
    >
    > > This is a multi-part message in MIME format.
    >
    > Why is this is a multi-part message in MIME format?
    >
    > Usenet is a plain text medium.
    >
    > Using MIME means that less people will see your posts.
    >
    > > I have an interesting problem...
    >
    > No, you have discovered a very fundamental aspect of computer science,
    > the difference between "code" and "data".
    >
    > > Perl seems to convert hex numbers which are explicit constants in the code
    >
    > Because then it is "code".
    >
    > > if $val is assigned from reading 0xFF from a file or user input,
    >
    > Because then it is "data".
    >
    > > $val is treated like a string instead of
    > > a number.
    >
    > Good, since it *is* a string instead of a number.
    >
    > > I am required to "use strict;" and so I get warnings
    >
    > "use strict" does NOT issue warnings.
    >
    > "use warnings" issues warnings.
    >
    > > The bottom line is how can I convert a string into an integer or a float??
    >
    > You can't.
    >
    > You can, however, convert a hex string into the corresponding
    > numerical value. The answer to that question ends up sounding
    > like you are joking with us...
    >
    > perldoc -f hex
    >
    > :-)
    >
    > --
    > Tad McClellan SGML consulting
    > [email]tadmcaugustmail.com[/email] Perl programming
    > Fort Worth, Texas
    Mark Fenbers Guest

  7. #7

    Default Re: string to integer

    On Thu, 18 Sep 2003 07:28:59 -0400, Mark Fenbers
    <Mark.Fenbersnoaa.gov> wrote:

    Don't top-post. Top-posting is rude and severely
    damages your chances of having your posts
    read by a competent professional. If you do it
    again you will be killfiled by everybody left worth
    listening to. Many have already done so.

    For posting guidelines ot comp.lang.perl.misc,
    read the regular Posting Guidelines:

    [url]http://mail.augustmail.com/~tadmc/clpmisc.shtml[/url]

    For more information about netiquette in general,
    see the "Netiquette Guidelines" at:

    [url]http://andrew2.andrew.cmu.edu/rfc/rfc1855.html[/url]
    >This is a multi-part message in MIME format.
    Don't do that. Usenet is a text-only medium.
    Posting in MIME is rude and severely
    damages your chances of having your posts
    read by a competent professional. If you do it
    again you will be killfiled by everybody left worth
    listening to. Many have already done so.
    >I don't post very often to newsgroups because there
    >always has to be some condescending know-it-all to
    >chime in his two cents.
    Yes, that describes you perfectly.
    >Someone always finds a way to be rude...
    Indeed.
    >Tad, your comments were critical, not helpful. *sigh* I guess I
    >expected better etiquette from a professional...
    Tad's comments are invariably helpful and professional.
    If you bothered to read them and follow them you would
    find out exactly how helpful they are. Instead you
    choose to whine and complain and derive no benefit
    at all.

    I could find not a single comment in his post which is
    not 100% accurate and if followed, would help
    you solve your problem quickly. Now go and
    read perldoc -f hex and stop whining.

    And don't top post in MIME again.
    --
    Helgi Briem hbriem AT simnet DOT is

    Excuse the munged address. My last
    e-mail address was killed by spammers.
    Helgi Briem Guest

  8. #8

    Default Re: string to integer

    Mark Fenbers <Mark.Fenbersnoaa.gov> wrote:
    > I don't post very often to newsgroups

    Then isn't it possible that there are aspects of Usenet that
    you are unaware of?

    I post to newsgroups rather often, so I _am_ aware of them.

    I thought I'd share what I have learned with you, but you seem
    resistant to learning how to get the most out of Usenet...

    > because there always has to be some
    > condescending know-it-all to chime in his two cents.

    Some folks will be able to forgive you for that, others will
    just ignore every post that you make in the future.

    > Someone always finds a way
    > to be rude...

    Posting MIME is seen as rude on Usenet.

    Top-posting is seen as rude on Usenet.

    Asking about hex values without even bothering to see if there is a
    function named hex() is seen as rude as well.

    People that _do_ use newsgroups often will recognize who is
    being rude here...

    > Tad, your comments were critical, not helpful.

    You needed the hex() function. I told you about the hex() function.
    How is that not helpful?

    Your posts are being ignored by the people most qualified to answer
    your questions, I told you how to avoid being ignored. That seems
    "helpful" to me too...

    > *sigh* I guess I
    > expected better etiquette from a professional...

    You can expect rudeness to be repaid in kind.

    If you hope to avoid rudeness, then avoid being rude yourself
    in the first place.


    --
    Tad McClellan SGML consulting
    [email]tadmcaugustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  9. #9

    Default Re: string to integer

    Helgi Briem wrote:
    >For posting guidelines ot comp.lang.perl.misc,
    >read the regular Posting Guidelines:
    >
    >[url]http://mail.augustmail.com/~tadmc/clpmisc.shtml[/url]
    Note to the OP: even though this page is written by and posted on the
    website of Tad McClellan, here also present, the regulars here have
    generally approved it. These aren't Tad's rules, these are OUR rules.

    --
    Bart.
    Bart Lateur Guest

  10. #10

    Default string to integer

    hi its me iffy..
    I want to convert a string comming from a PHP file
    e.g .....
    <?
    $var = "some value212" ;
    print "content=$var";
    ?>
    then i call this variable in Flash through LoadVariableNum Function....
    i m trying to use this variable in a loop...
    foo = 0;
    while (foo<j) {
    duplicateMovieClip("_root.cross", "cross2"+foo, foo);
    setProperty("cross2"+foo, _x, 160+random(275));
    setProperty("cross2"+foo, _y, 45+random(190));
    foo++;
    }
    I want to use this variable in replace of "J" ................ but when i
    trace this value (content) in Flash.. it gives an undefined value...
    should i replave this content value to integer... if so.. then how can i
    convert this value to integer.... ?
    what i should do ????


    iffy_aris Guest

  11. #11

    Default Re: string to integer

    You can convert string to number using...

    Number(value);
    adireddy Guest

Similar Threads

  1. check string rep's integer
    By sinister in forum PHP Development
    Replies: 7
    Last Post: November 24th, 09:27 AM
  2. ado xtra - integer variable in sql-string
    By janosch webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 2
    Last Post: October 14th, 10:18 PM
  3. php array problem string to integer
    By Sébastien in forum PHP Development
    Replies: 0
    Last Post: August 3rd, 06:17 AM
  4. HowTo check if my String is a Number (Integer) ?
    By Craig Deelsnyder in forum ASP.NET
    Replies: 0
    Last Post: July 27th, 11:06 PM
  5. Equating a String & Integer Variable
    By Roger Lord in forum ASP.NET General
    Replies: 2
    Last Post: July 3rd, 02:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139