MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Driving me nuts

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33816] Re: [mg33801] Driving me nuts
  • From: BobHanlon at aol.com
  • Date: Fri, 19 Apr 2002 02:27:33 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

In a message dated 4/16/02 5:42:23 AM, stevebg at adelphia.net writes:

>    The following simple function does not do anything
>when called except echo its name.
>    Any tips will be appreciated, to put it mildly.
>
>listr[lst] := Module[{lct, itm, str},
>    str = ".";
>    For [ lct = 1, lct <= Length[lst], lct++,
>                { itm = lst[[lct]];
>                   If[itm <= 9, itm = itm + 48, itm = itm + 55];
>                    str = StringInsert[str, FromCharacterCode[itm], 1];
>                }]
>      Return[Reverse[str]];
>    ]
>
>foo = {1, 4, 14, 23}
>listr[foo]
>listr[{1, 4, 14, 23}]
>
>The last line shows what happens. No errors are reported.
>

1. The LHS of the definition should use a pattern.
2. You must use StringReverse rather than Reverse to reverse a string. 
However, you don't need StringReverse if you put the character in the proper 
position initially.
3. Return is unnecessary for normal termination of a function.
4. The output should not be suppressed if you want to see the results.

listr[lst_]:=
    Module[{lct,itm,str},
      str=".";
      For[lct=1,lct<=Length[lst],lct++,
        itm=lst[[lct]];
        If[itm<=9,itm=itm+48,itm=itm+55];
        str=StringInsert[str,FromCharacterCode[itm],-1]];
      str];


foo={1,4,14,23};

listr[foo]

.14EN

Or you could write this more simply as

listr2[lst_] := "."<>FromCharacterCode[If[#<=9,#+48,#+55]& /@ lst];

listr2[foo]

.14EN


Bob Hanlon
Chantilly, VA  USA


  • Prev by Date: Re: Graphics Stopped Displaying on Screen
  • Next by Date: Re: Trouble calculating a volume
  • Previous by thread: Re: Driving me nuts
  • Next by thread: More accuracy in Disk