comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » concatenating strings
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
concatenating strings [message #85693] Tue, 27 August 2013 15:56 Go to next message
Seb is currently offline  Seb
Messages: 15
Registered: January 2006
Junior Member
Hi,

I just began learning IDL, and am not understanding why the following is
not producing the string (20111021):

---<--------------------cut here---------------start------------------->---
IDL> caldat, julday(1, 294, 2011), mon, day, year
% Compiled module: CALDAT.
IDL> print, year, mon, day
2011 10 21
IDL> print, string(year) + string(mon) + string(day)
2011 10 21
---<--------------------cut here---------------end--------------------->---

I thought that the string() function would coerce the numbers to a
string, and then the concatenation operator ('+') would join them
together, but this is not happening. What am I missing?

Cheers,

--
Seb
Re: concatenating strings [message #85694 is a reply to message #85693] Tue, 27 August 2013 16:11 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 8/27/13 4:56 PM, Seb wrote:
> Hi,
>
> I just began learning IDL, and am not understanding why the following is
> not producing the string (20111021):
>
> ---<--------------------cut here---------------start------------------->---
> IDL> caldat, julday(1, 294, 2011), mon, day, year
> % Compiled module: CALDAT.
> IDL> print, year, mon, day
> 2011 10 21
> IDL> print, string(year) + string(mon) + string(day)
> 2011 10 21
> ---<--------------------cut here---------------end--------------------->---
>
> I thought that the string() function would coerce the numbers to a
> string, and then the concatenation operator ('+') would join them
> together, but this is not happening. What am I missing?
>
> Cheers,
>

It is happening, but the individual strings that are being created are
being created with the default formatting (with leading spaces). Try:

IDL> print, strtrim(year, 2) + strtrim(mon, 2) + strtrim(day, 2)
20111021

Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
Re: concatenating strings [message #85695 is a reply to message #85694] Tue, 27 August 2013 16:31 Go to previous messageGo to next message
Seb is currently offline  Seb
Messages: 15
Registered: January 2006
Junior Member
On Tue, 27 Aug 2013 17:11:48 -0600,
Michael Galloy <mgalloy@gmail.com> wrote:

> On 8/27/13 4:56 PM, Seb wrote:
>> Hi,

>> I just began learning IDL, and am not understanding why the following
>> is not producing the string (20111021):

>> ---<--------------------cut
>> here---------------start------------------->---
IDL> caldat, julday(1, 294, 2011), mon, day, year
>> % Compiled module: CALDAT.
IDL> print, year, mon, day
>> 2011 10 21
IDL> print, string(year) + string(mon) + string(day)
>> 2011 10 21 ---<--------------------cut
>> here---------------end--------------------->---

>> I thought that the string() function would coerce the numbers to a
>> string, and then the concatenation operator ('+') would join them
>> together, but this is not happening. What am I missing?

>> Cheers,


> It is happening, but the individual strings that are being created are
> being created with the default formatting (with leading spaces). Try:

IDL> print, strtrim(year, 2) + strtrim(mon, 2) + strtrim(day, 2)
> 20111021

Thank you. So the leading spaces are created by the 'caldat' function
when it outputs the variables, right?


--
Seb
Re: concatenating strings [message #85696 is a reply to message #85694] Tue, 27 August 2013 16:32 Go to previous messageGo to next message
Phillip Bitzer is currently offline  Phillip Bitzer
Messages: 223
Registered: June 2006
Senior Member
Just to add a little hint, try using help:

IDL>help, STRING(year)
<Expression> STRING = ' 2011'

Now, you can clearly see the blanks.

And yes, "+" is doing concatenation:
IDL> help, string(year) + string(mon) + string(day)
<Expression> STRING = ' 2011 10 21'

A piece of advice, since you're starting out: be careful with "print". Sometimes, you think something has gone terribly wrong, when in fact everything's OK:
IDL> a = 14.12345
IDL> print, a
14.1235

Did IDL truncate my number? No, it's just a limitation of the default print format:
IDL> print, a, FORMAT='(F10.6)'
14.123450
Re: concatenating strings [message #85697 is a reply to message #85696] Wed, 28 August 2013 06:29 Go to previous messageGo to next message
Seb is currently offline  Seb
Messages: 15
Registered: January 2006
Junior Member
On Tue, 27 Aug 2013 16:32:41 -0700 (PDT),
Phillip Bitzer <bitzerp@uah.edu> wrote:

> Just to add a little hint, try using help:
IDL> help, STRING(year)
> <Expression> STRING = ' 2011'

> Now, you can clearly see the blanks.

> And yes, "+" is doing concatenation:
IDL> help, string(year) + string(mon) + string(day)
> <Expression> STRING = ' 2011 10 21'

> A piece of advice, since you're starting out: be careful with
> "print". Sometimes, you think something has gone terribly wrong, when
> in fact everything's OK:
IDL> a = 14.12345 print, a
> 14.1235

> Did IDL truncate my number? No, it's just a limitation of the default
> print format:
IDL> print, a, FORMAT='(F10.6)'
> 14.123450

Thanks both of you for these pointers. I'm learning via a combination
of reading documentation/books and reviewing/editing code I've
inherited.

Cheers,

--
Seb
Re: concatenating strings [message #85702 is a reply to message #85695] Wed, 28 August 2013 14:58 Go to previous message
kagoldberg is currently offline  kagoldberg
Messages: 26
Registered: November 2012
Junior Member
> So the leading spaces are created by the 'caldat' function when it outputs the variables, right?
This is not correct. The mon, day, year values are numbers, not strings, so they have no inherent spaces. The leading spaces are inserted when you convert them to strings without specifying a formatting for string() to use.

You could choose to format them like this
string(year, FORMAT='(i4.4)') + string(mon, FORMAT='(i2.2)') + string(day, FORMAT='(i2.2)') or
string(year, mon, day, FORMAT='(i4.4,i2.2,i2.2)')

the code "i2" means you want an integer that has 2 digits (including possible leading space), and the ".2" means to pad single digit results with leading zeros to make the number always have 2 digits. Similar for the "i4.4"
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: What subprogram? What parameters and keywords?
Next Topic: Emacs IDLWAVE

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:50:22 PDT 2025

Total time taken to generate the page: 0.00611 seconds