Re: Simple number formatting problem [message #20993] |
Thu, 03 August 2000 00:00 |
Luis Alonso
Messages: 27 Registered: February 2000
|
Junior Member |
|
|
> p.s. F95 has a new formatting option, i0, which would print out 1 as
> "1", 10 as "10" etc. i.e. the leading blanks removed. Maybe this should
> go on a future IDL feature request list....? :o)
strtrim do remove leading blanks. and it would not be longer to write than
the Format thing.
What i really thougt is that the print command shoud print numbers without
any kind of formatting (means no leading blanks, no cutting out decimals...)
unless the format command is used.
It is a pain to keep on writing that strtrim stuff...
And when i need formated print i actually use the format keyword.
That's one other wish for me. Although i know it won't be wellcomed because
of back compatibility
Greets
Luis Alonso
|
|
|
Re: Simple number formatting problem [message #21016 is a reply to message #20993] |
Wed, 02 August 2000 00:00  |
Ben Tupper
Messages: 186 Registered: August 1999
|
Senior Member |
|
|
Paul van Delst wrote:
>
> Good lord! Maybe it's coz you use a Mac....?? :o)
>
>
Actually, I wrote it when I was on windows/unix. It is not elegant but it
is handy.
Ben
--
Ben Tupper
Bigelow Laboratory for Ocean Science
West Boothbay Harbor, Maine
btupper@bigelow.org
note: email address new as of 25JULY2000
|
|
|
Re: Simple number formatting problem [message #21017 is a reply to message #21016] |
Wed, 02 August 2000 00:00  |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
Ben Tupper wrote:
>
> Hello,
>
> I been down this street before. Try using the PARSE_FILENAME() function
> below. I also added a sort_extension rotuine which worked on the files
> called Untitled.01, Untitled.12, untitled.03
[bunch of stuff snipped...]
Good lord! Maybe it's coz you use a Mac....?? :o)
The Fortran news group has regular questions about this exact same
thing. As David's answer points out, the use of iw.w formatting is oh so
helpful.
paulv
p.s. F95 has a new formatting option, i0, which would print out 1 as
"1", 10 as "10" etc. i.e. the leading blanks removed. Maybe this should
go on a future IDL feature request list....? :o)
--
Paul van Delst Ph: (301) 763-8000 x7274
CIMSS @ NOAA/NCEP Fax: (301) 763-8545
Rm.202, 5200 Auth Rd. Email: pvandelst@ncep.noaa.gov
Camp Springs MD 20746
|
|
|
Re: Simple number formatting problem [message #21018 is a reply to message #21016] |
Wed, 02 August 2000 00:00  |
Ben Tupper
Messages: 186 Registered: August 1999
|
Senior Member |
|
|
Hello,
I been down this street before. Try using the PARSE_FILENAME() function
below. I also added a sort_extension rotuine which worked on the files
called Untitled.01, Untitled.12, untitled.03
Here's the output of sort_extension:
IDL> sort_extension
Macintosh HD:pemaquid:untitled.01has extension 01
Which is a float value of 1.00000
And is position 0 in the file list
Macintosh HD:pemaquid:untitled.03has extension 03
Which is a float value of 3.00000
And is position 1 in the file list
Macintosh HD:pemaquid:untitled.12has extension 12
Which is a float value of 12.0000
And is position 2 in the file list
+++++START
;+
; NAME:
; PARSE_FILENAME
;
; PURPOSE:
; This function returns a 3 column array of a parsed filename.
; Each column contains the path, filename and extension respectively.
;
; CATEGORY:
; String processing.
;
; CALLING SEQUENCE:
; result = PARSE_FILENAME(Filename)
;
; INPUTS:
; Filename : a string or array of strings of the complete path and
; extension to a file.
;
;
;
; OUTPUTS:
; This function returns a 3,n string array. the number of rows is
determined
; by the number of elements in the input parameter.
; F = PICKFILE(/READ, FILTER = ['pro', 'dat'])
;
; RESTRICTIONS:
; Directories are divided by a forward slash (/) in UNIX or a
; backslash (\) in WINDOWS or a colon (:) in MACOS.
; Three character file extensions are appended with a dot (.).
; MODIFICATION HISTORY:
; Written by: Ben Tupper, August, 1999
;
; 26 SEP 99 Increased Dot position by one
; 18 JAN 00 Corrected how the FileName is extracted
; when there is no '.ext'
;-
FUNCTION parse_filename, filelist
OS = StrLowCase( !version.os_family)
case os of
'unix': separator = '/'
'windows': separator = '\'
'macos': separator = ':'
endcase
n = n_elements(filelist) ; get the number of files that qualify
If n LT 1 Then Return,-1
slash = rstrpos(filelist,separator) +1 ; get the last slash position plus
one
dot = strpos(filelist,'.') ; get the dot position
len = dot - slash ; calc the length of the name
parsed = strarr(3,n)
;get the filename
for i = 0,n-1 do $
If Dot[i] NE -1 Then parsed[1,i] = strmid(filelist[i],slash[i], len[i])
Else $
Parsed[1,i] = StrMid(filelist[i],slash[i])
len = slash ;get the path
for i = 0,n-1 do parsed[0,i] = strmid(filelist[i],0, len[i])
len = strlen(filelist) - dot-1
; get the extension if it exists
For i = 0,N-1 Do Begin
If Dot[i] NE -1 Then $
parsed[2,i] = strmid(filelist[i],dot[i]+1, len[i]) $
Else $
parsed[2,i] = Strmid(filelist[i],Slash[i], len[i] - Slash[i])
EndFor
return, parsed
end
PRO SORT_Extension
Files = FindFile('Macintosh HD:pemaquid:untitled*')
Parsed = Parse_FileName(Files)
Floated = Float(StrTrim(Parsed[2,*],2))
Sorted = Sort(Floated)
For i = 0, N_elements(Files)-1 Do Begin
Print, Files[i] + 'has extension ' + Parsed[2,i]
Print, 'Which is a float value of ', Floated[i]
Print, 'And is position ', Sorted[i], ' in the file list'
EndFor
END
++++++END
Simon de Vet wrote:
> Trivial problem, no doubt, but I've never been able to get my brane
> around formatting options.
>
> I have a list of files, with the extension .01, .02, .03 ... .12. The
> preceding portion is indentical in all cases.
>
> I'd like to be able to load these in a loop, but I first need to convert
> the counter value into this format.
>
> My current, and very ugly, technique is to have an array filled with
> ['01', '02', ... '12'], and then reference the appropriate entry. It
> works, but I don't like it.
>
> Any ideas?
>
> Simon
--
Ben Tupper
Bigelow Laboratory for Ocean Science
West Boothbay Harbor, Maine
btupper@bigelow.org
note: email address new as of 25JULY2000
|
|
|
Re: Simple number formatting problem [message #21020 is a reply to message #21016] |
Wed, 02 August 2000 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Simon de Vet (simon@mathstat.dal.ca) writes:
> Trivial problem, no doubt, but I've never been able to get my brane
> around formatting options.
>
> I have a list of files, with the extension .01, .02, .03 ... .12. The
> preceding portion is indentical in all cases.
>
> I'd like to be able to load these in a loop, but I first need to convert
> the counter value into this format.
>
> My current, and very ugly, technique is to have an array filled with
> ['01', '02', ... '12'], and then reference the appropriate entry. It
> works, but I don't like it.
I'd try something like this:
IDL> base = 'myfile'
IDL> ext = '.dat'
IDL> for j=1,12 do print, base + String(j, Format="(I2.2)") + ext
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|