How to avoid number to string auto conversion?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to avoid number to string auto conversion?

Sean-130

Hello,

Long story short:

let readonly_list = ['0000 AAAA', '0010 BBBB']
let readonly_pat = "^" . 0010

let my_match_index = match(readonly_list, readonly_pat)

The above result is -1.
But what I want is 1 (the 2nd item on the list)

It looks that the readonly_pat is "transferred" automatically to
another string "^8", as string(0010)='8'.

Is it possible to avoid this typical behavior from scripting language?

Thanks

Sean

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

RE: How to avoid number to string auto conversion?

JohnBeckett

Sean wrote:
> let readonly_list = ['0000 AAAA', '0010 BBBB']
> let readonly_pat = "^" . 0010

But the 0010 is parsed as a NUMBER and the result is appended to "^".

In Vim, the following will display 8:
  :echo 010

because (according to the infallible law of the C language), a number
beginning with a zero digit is octal.

Where did the 0010 come from? Is it in a variable (with value decimal
10)? Any you want it put into a four-digit string with leading zeroes?
If so, try (untested):

  let readonly_pat = '^' . printf('%04d', myvariable)

John


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to avoid number to string auto conversion?

Sean-130



On Jan 10, 6:44 pm, "John Beckett" <[hidden email]> wrote:

> Sean wrote:
> > let readonly_list = ['0000 AAAA', '0010 BBBB']
> > let readonly_pat = "^" . 0010
>
> But the 0010 is parsed as a NUMBER and the result is appended to "^".
>
> In Vim, the following will display 8:
>   :echo 010
>
> because (according to the infallible law of the C language), a number
> beginning with a zero digit is octal.
>
> Where did the 0010 come from? Is it in a variable (with value decimal
> 10)? Any you want it put into a four-digit string with leading zeroes?
> If so, try (untested):
>
>   let readonly_pat = '^' . printf('%04d', myvariable)
>
> John

That is genius!

let myvariable = 0010
printf('%04d',myvariable)='0008'
printf('%04o',myvariable)='0010'

The last is what I really want!

The data are from a data file like:
0000 AAAA
0000 AAAB
0010 BBBB

The number is <key>, the <value> is multi-byte, and it is one to many
mapping.
BTW, there are hundreds, if not thousands, of 'invention' to do such
kind of mapping.

This is for vimim.vim plugin.

Thanks for your inspiration!

Sean
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

RE: How to avoid number to string auto conversion?

JohnBeckett

Sean wrote:

> let myvariable = 0010
> printf('%04d',myvariable)='0008'
> printf('%04o',myvariable)='0010'
>
> The last is what I really want!
>
> The data are from a data file like:
> 0000 AAAA
> 0000 AAAB
> 0010 BBBB
>
> The number is <key>, the <value> is multi-byte, and it is one
> to many mapping.

I don't see why you would want octal. If you like, post a little more
sample code to show what you actually want to do. Are you trying to read
the data file (as above), and put the data into a structure? How about a
dictionary of lists? Where does the octal come in?

When you look up the data, what is the input, and what is the wanted
output? For example:

 let mykey = 0  " the 0000 above
 let mylist = Lookup(mykey)  " list ['AAAA', 'AAAB']

John


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to avoid number to string auto conversion?

Tony Mechelynck
In reply to this post by Sean-130

On 11/01/09 02:30, Sean wrote:

> Hello,
>
> Long story short:
>
> let readonly_list = ['0000 AAAA', '0010 BBBB']
> let readonly_pat = "^" . 0010
>
> let my_match_index = match(readonly_list, readonly_pat)
>
> The above result is -1.
> But what I want is 1 (the 2nd item on the list)
>
> It looks that the readonly_pat is "transferred" automatically to
> another string "^8", as string(0010)='8'.
>
> Is it possible to avoid this typical behavior from scripting language?
>
> Thanks
>
> Sean

Use a string in the first place:

        :let readonly_pat = '^0010'
or
        :let readonly_pat = '^' . '0010'
or
        :let i = 10 " not 0010 which is eight
        :let readonly_pat = '^' . printf ('%4d', i)

Note that if you generate successive numbers as ... 0007, 0008, 0009,
0010, 0011, ... you'll have problems if you try to treat them later as
octal values, where you'll get ... 007, 010, 011, 010, 011, ...

Best regards,
Tony.
--
At Group L, Stoffel oversees six first-rate programmers, a managerial
challenge roughly comparable to herding cats.
                -- The Washington Post Magazine, 9 June, 1985

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to avoid number to string auto conversion?

Sean-130
In reply to this post by JohnBeckett


On Jan 10, 11:07 pm, "John Beckett" <[hidden email]> wrote:

> Sean wrote:
> > let myvariable = 0010
> > printf('%04d',myvariable)='0008'
> > printf('%04o',myvariable)='0010'
>
> > The last is what I really want!
>
> > The data are from a data file like:
> > 0000 AAAA
> > 0000 AAAB
> > 0010 BBBB
>
> > The number is <key>, the <value> is multi-byte, and it is one
> > to many mapping.
>
> I don't see why you would want octal. If you like, post a little more
> sample code to show what you actually want to do. Are you trying to read
> the data file (as above), and put the data into a structure? How about a
> dictionary of lists? Where does the octal come in?
>
> When you look up the data, what is the input, and what is the wanted
> output? For example:
>
>  let mykey = 0  " the 0000 above
>  let mylist = Lookup(mykey)  " list ['AAAA', 'AAAB']
>
> John


Thanks John, it already worked using
printf('%04o',myvariable)='0010'

What I have is 0010 in the data file.
What I want is '0010' in the list and during conversion.
The printf() you pointed out worked!


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---