Re: anything like sprintf? [message #58813] |
Mon, 18 February 2008 09:11 |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On Feb 18, 6:52 am, Yann Zoll <y_z2...@yahoo.de> wrote:
> May the force be with you !!
> Thats what i was lokking for, thank you.
You can use C-style format codes with a bit more notation (since you
mentioned sprintf, I'm assuming you like those):
IDL> time = string(format='(%"%02d.%02d.%02d")',hh,mm,ss)
This is a direct translation of you sprintf statement.
Mike
--
www.michaelgalloy.com
Tech-X Corporation
Software Developer II
|
|
|
Re: anything like sprintf? [message #58814 is a reply to message #58813] |
Mon, 18 February 2008 05:52  |
Yann Zoll
Messages: 4 Registered: February 2008
|
Junior Member |
|
|
>
> hour = 2
> minute = 3
> second=9
> time = string([hour,minute,second],$
> format='(I02,":",I02,":",I02)')
>
May the force be with you !!
Thats what i was lokking for, thank you.
Yann
|
|
|
Re: anything like sprintf? [message #58815 is a reply to message #58814] |
Mon, 18 February 2008 05:35  |
lasse
Messages: 48 Registered: February 2007
|
Member |
|
|
On 18 Feb, 13:56, Yann Zoll <y_z2...@yahoo.de> wrote:
> Thx for all the sugestions.
>
> At the moment i use the if conditions. you need one if... per value:
>
> if for example the problem is the time:
>
> [/color]
[color=blue]> [/color]
[color=blue]> hh, mm, ss = hours, minutes, seconds[/color]
[color=blue]> [/color]
[color=blue]> if hh lt 10 then hh = '0'+string(hh)[/color]
[color=blue]> if mm lt 10 ....[/color]
[color=blue]> if ss ...[/color]
[color=blue]> [/color]
[color=blue]> time = strcompress(hh+':'+mm+':'ss',/REMOVE_ALL)[/color]
[color=blue]> [/color]
[color=blue]> [/color]
>
> if you encounter this problem in perl, the code looks like:
>
> [/color]
[color=blue]> [/color]
[color=blue]> time = sprintf("%02d.%02d.%02d",hh,mm,ss)[/color]
[color=blue]> [/color]
[color=blue]> [/color]
>
> ok, perl needs no explicit type casting as idl does so the "string()"
> command is necessary, but is it possible to assign the output of print
> (with the format keyword) to a variable?
>
> yann
No, but you can use a format code for the conversion of a number to a
string by specifying the format keyword, as in my example above.
Again, read the IDL help on format codes. IDL uses Fortran format
codes which are very similar to the stuff you already use in Perl with
sprintf. Your example in IDL with format codes would be
hour = 2
minute = 3
second=9
time = string([hour,minute,second],$
format='(I02,":",I02,":",I02)')
As you can see, the three numbers are converted to a string using the
STRING() function. Additionally, you can pass a format code, here I02.
"I" means that the output is an integer value, i.e. no positions after
the decimal point. "2" is the length of the output and "0" indicates
the padding with 0 if the integer output is shorter than the length.
Read here http://idlastro.gsfc.nasa.gov/idl_html_help/Format_Codes.htm l.
On that page you will see that IDL also supports C-style formatting
which is the formatting Perl uses.
So long
Lasse Clausen
|
|
|
Re: anything like sprintf? [message #58816 is a reply to message #58815] |
Mon, 18 February 2008 05:20  |
Haje Korth
Messages: 651 Registered: May 1997
|
Senior Member |
|
|
Try format code format='(i2.2)'
Haje
"Yann Zoll" <y_z2000@yahoo.de> wrote in message
news:fpbpsb$tlp$1@inlux3.rz.uni-kiel.de...
> Hi newsgroup.
>
> I just wonder if there is any good looking way to do apply a preceding
> zero to a string if value is less than 10.
> To build a time string from integer values of hour, minute, second.
> In perl there is the sprintf command to do this. Is there anything like
> this in IDL?
>
> Thx, Yann
|
|
|
Re: anything like sprintf? [message #58817 is a reply to message #58816] |
Mon, 18 February 2008 04:56  |
Yann Zoll
Messages: 4 Registered: February 2008
|
Junior Member |
|
|
Thx for all the sugestions.
At the moment i use the if conditions. you need one if... per value:
if for example the problem is the time:
hh, mm, ss = hours, minutes, seconds
if hh lt 10 then hh = '0'+string(hh)
if mm lt 10 ....
if ss ...
time = strcompress(hh+':'+mm+':'ss',/REMOVE_ALL)
if you encounter this problem in perl, the code looks like:
time = sprintf("%02d.%02d.%02d",hh,mm,ss)
ok, perl needs no explicit type casting as idl does so the "string()"
command is necessary, but is it possible to assign the output of print
(with the format keyword) to a variable?
yann
|
|
|
Re: anything like sprintf? [message #58818 is a reply to message #58817] |
Mon, 18 February 2008 04:40  |
lasse
Messages: 48 Registered: February 2007
|
Member |
|
|
On 18 Feb, 12:28, Yann Zoll <y_z2...@yahoo.de> wrote:
> Hi newsgroup.
>
> I just wonder if there is any good looking way to do apply a preceding
> zero to a string if value is less than 10.
> To build a time string from integer values of hour, minute, second.
> In perl there is the sprintf command to do this. Is there anything like
> this in IDL?
>
> Thx, Yann
Hi,
read about format codes in the IDL manual. There you will find that
x = 4
print, string(x, format='(I02)')
will do exactly that. Personally, I love format codes and that it is
well worth the effort to understand how to use them.
Cheers
Lasse Clausen
|
|
|
Re: anything like sprintf? [message #58819 is a reply to message #58818] |
Mon, 18 February 2008 03:43  |
david[4]
Messages: 5 Registered: January 2008
|
Junior Member |
|
|
In this cases i use the following instruction:
IF (strlen(svalue) eq 1) THEN svalue='0'+svalue
Where value must be the string representing your value without SPACES.
svalue=STRTRIM(value,2)
But is not very good looking i think..
Yann Zoll wrote:
> Hi newsgroup.
>
> I just wonder if there is any good looking way to do apply a preceding
> zero to a string if value is less than 10.
> To build a time string from integer values of hour, minute, second.
> In perl there is the sprintf command to do this. Is there anything like
> this in IDL?
>
> Thx, Yann
>
|
|
|
Re: anything like sprintf? [message #58820 is a reply to message #58819] |
Mon, 18 February 2008 03:43  |
Spon
Messages: 178 Registered: September 2007
|
Senior Member |
|
|
On Feb 18, 11:28 am, Yann Zoll <y_z2...@yahoo.de> wrote:
> Hi newsgroup.
>
> I just wonder if there is any good looking way to do apply a preceding
> zero to a string if value is less than 10.
> To build a time string from integer values of hour, minute, second.
> In perl there is the sprintf command to do this. Is there anything like
> this in IDL?
>
> Thx, Yann
Are you looking for something as straightforward as this?
if fix(a) lt 10 then a = '0' + a
|
|
|