Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #11629 > unrolled thread

Transforming/expanding a list

Started byMark Coleman <markspcoleman@gmail.com>
First post2012-09-06 08:14 +0000
Last post2012-09-07 09:01 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Transforming/expanding a list Mark Coleman <markspcoleman@gmail.com> - 2012-09-06 08:14 +0000
    Re: Transforming/expanding a list Murta <rodrigomurtax@gmail.com> - 2012-09-07 08:56 +0000
    Re: Transforming/expanding a list awnl <awnl@gmx-topmail.de> - 2012-09-07 08:57 +0000
    Re: Transforming/expanding a list Ray Koopman <koopman@sfu.ca> - 2012-09-07 09:01 +0000

#11629 — Transforming/expanding a list

FromMark Coleman <markspcoleman@gmail.com>
Date2012-09-06 08:14 +0000
SubjectTransforming/expanding a list
Message-ID<k29m0t$a8r$1@smc.vnet.net>
I'm working on some Mathematica code that will help transform a large set of lists drawn from my company's legacy database systems. A typical row of the raw data looks like

{x1,NULL,NULL,x2,NULL,NULL,NULL,NULL,x3,NULL,NULL,x4,x5,NULL} ...

The lists are sequences of real numbers, separating some some number of Strings which for convenicence sake we call "NULL" (as opposed to a proper Mathematica Null). The goal is to create a new list where each NULL is replaced by the real number that most recently proceeds it (note that any number of NULLs may occur before or after any real values). I would like the code to return

{x1,x1,x1,x2,x2,x2,x2,x2,x3,x3,x3,x4,x5,x5}


Thanks

Mark

[toc] | [next] | [standalone]


#11642

FromMurta <rodrigomurtax@gmail.com>
Date2012-09-07 08:56 +0000
Message-ID<k2ccrg$f17$1@smc.vnet.net>
In reply to#11629
Hi Mark
There is one suggenstion

data={"x1",Null,Null,"x2",Null,Null,Null,Null,"x3",Null,Null,"x4","x5",Null}

ConstantArray[#[[1]],Length[#]]&/@Split[data,(#2==Null)&]//Flatten

{"x1", "x1", "x1", "x2", "x2", "x2", "x2", "x2", "x3", "x3", "x3", "x4", "x5", "x5"}

best Regards
Murta

On Thursday, September 6, 2012 5:15:28 AM UTC-3, Mark Coleman wrote:
> I'm working on some Mathematica code that will help transform a large set of lists drawn from my company's legacy database systems. A typical row of the raw data looks like
>
>
>
> {x1,NULL,NULL,x2,NULL,NULL,NULL,NULL,x3,NULL,NULL,x4,x5,NULL} ...
>
>
>
> The lists are sequences of real numbers, separating some some number of Strings which for convenicence sake we call "NULL" (as opposed to a proper Mathematica Null). The goal is to create a new list where each NULL is replaced by the real number that most recently proceeds it (note that any number of NULLs may occur before or after any real values). I would like the code to return
>
>
>
> {x1,x1,x1,x2,x2,x2,x2,x2,x3,x3,x3,x4,x5,x5}
>
>
>
>
>
> Thanks
>
>
>
> Mark

[toc] | [prev] | [next] | [standalone]


#11647

Fromawnl <awnl@gmx-topmail.de>
Date2012-09-07 08:57 +0000
Message-ID<k2ccue$f2r$1@smc.vnet.net>
In reply to#11629
Hi,

> I'm working on some Mathematica code that will help transform a large
> set of lists drawn from my company's legacy database systems. A
> typical row of the raw data looks like
>
> {x1,NULL,NULL,x2,NULL,NULL,NULL,NULL,x3,NULL,NULL,x4,x5,NULL} ...
>
> The lists are sequences of real numbers, separating some some number
> of Strings which for convenicence sake we call "NULL" (as opposed to
> a proper Mathematica Null). The goal is to create a new list where
> each NULL is replaced by the real number that most recently proceeds
> it (note that any number of NULLs may occur before or after any real
> values). I would like the code to return
>
> {x1,x1,x1,x2,x2,x2,x2,x2,x3,x3,x3,x4,x5,x5}
>

I think this will do what you need:

row = {x1, NULL, NULL, x2, NULL, NULL, NULL, NULL, x3, NULL, NULL, x4,
     x5, NULL};

FoldList[Replace[#2, NULL :> #1] &, First[row], Rest[row]]

hth,

albert

[toc] | [prev] | [next] | [standalone]


#11654

FromRay Koopman <koopman@sfu.ca>
Date2012-09-07 09:01 +0000
Message-ID<k2cd5f$f5l$1@smc.vnet.net>
In reply to#11629
On Sep 6, 1:15 am, Mark Coleman <markspcole...@gmail.com> wrote:
> I'm working on some Mathematica code that will help transform a large set of lists drawn from my company's legacy database systems. A typical row of the raw data looks like
>
> {x1,NULL,NULL,x2,NULL,NULL,NULL,NULL,x3,NULL,NULL,x4,x5,NULL} ...
>
> The lists are sequences of real numbers, separating some some number of Strings which for convenicence sake we call "NULL" (as opposed to a proper Mathematica Null). The goal is to create a new list where each NULL is replaced by the real number that most recently proceeds it (note that any number of NULLs may occur before or after any real values). I would like the code to return
>
> {x1,x1,x1,x2,x2,x2,x2,x2,x3,x3,x3,x4,x5,x5}
>
> Thanks
>
> Mark

data = {x1,NULL,NULL,x2,NULL,NULL,NULL,NULL,x3,NULL,NULL,x4,x5,NULL};

x = x0 (* just in case the first element in data is NULL *);
If[# === NULL, x, x = #]& /@ data

{x1, x1, x1, x2, x2, x2, x2, x2, x3, x3, x3, x4, x5, x5}

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web