|
|||
|
I have a list (see below) I think should be stored in arrays.
However, it seems it will be easier to simply use NR to record each line instead of trying to get a multidimensional array simulation so that each month gets all days associated with it, perhaps like : 01_01_2012 02_01_2012 14_01_2012 ....etc. example day/month/year list: 01 01 2012 02 01 2012 14 01 2012 22 01 2012 02 02 2012 03 02 2012 ....etc. Other suggestions appreciated. -Bryan |
|
|
||||
|
||||
|
|
|
|||
|
On 2/26/2012 11:39 AM, bryanlepore@gmail.com wrote:
> I have a list (see below) I think should be stored in arrays. Why? > However, it seems it will be easier to simply use NR to record each line instead of trying to get a multidimensional array simulation so that each month gets all days associated with it, perhaps like : > > 01_01_2012 > 02_01_2012 > 14_01_2012 > > ...etc. > > example day/month/year list: > 01 01 2012 > 02 01 2012 > 14 01 2012 > 22 01 2012 > 02 02 2012 > 03 02 2012 > ...etc. > > Other suggestions appreciated. It's very difficult to suggest a solution as you haven't described the problem, just told us what you think might be a solution. If you post some small sample input set with the expected output from that input and a brief description of what you're trying to do, THEN you can get a sensible suggestion for a solution. Otherwise you'd just be getting vague guesses that may be totally inappropriate for your problem. Ed. |
|
|||
|
On Feb 26, 1:09*pm, Ed Morton <mortons...@gmail.com> wrote:
> post some small sample input set with the expected output from that input > and a brief description of what you're trying to do this is changing every time I think about it, but data file : 01 01 2012 1234.23 02 01 2012 3.23 02 01 2012 45.11 14 01 2012 23.51 22 01 2012 526.67 22 01 2012 5.78 02 02 2012 1.67 03 02 2012 -3.69 I think I want to make one array per month ($2) that contains each day ($1) associated with each value $4. AFAIK at least arrays in awk can/ should do that, though I have considered using a more appropriate program. but this looks very amenable to awk. -Bryan |
|
|||
|
On 2/26/2012 12:30 PM, B wrote:
> On Feb 26, 1:09 pm, Ed Morton<mortons...@gmail.com> wrote: >> post some small sample input set with the expected output from that input >> and a brief description of what you're trying to do sample input - posted expected output - not posted brief description - not posted > > this is changing every time I think about it, but data file : > > 01 01 2012 1234.23 > 02 01 2012 3.23 > 02 01 2012 45.11 > 14 01 2012 23.51 > 22 01 2012 526.67 > 22 01 2012 5.78 > 02 02 2012 1.67 > 03 02 2012 -3.69 > > I think I want to make one array per month ($2) that contains each day > ($1) associated with each value $4. AFAIK at least arrays in awk can/ > should do that, though I have considered using a more appropriate > program. but this looks very amenable to awk. > > -Bryan Again, though, you're not telling us WHAT you're trying to do you're just telling us HOW you think you should do it. I can easily say that to the solution you describe above could be implemented as: { arr[$2] = arr[$2] $1 " " $4 ";" } but I have absolutely no idea if that solution is a good way to solve your problem or not as I still don't know what the problem actually IS that you're trying to solve. If you can't at least post some expected output from the above input and a brief description of why that's the output, then it's not worth your while posting here as the answers you get could be totally wrong for your actual application. Ed. |
|
|||
|
On 2/26/2012 12:52 PM, Ed Morton wrote:
> On 2/26/2012 12:30 PM, B wrote: >> On Feb 26, 1:09 pm, Ed Morton<mortons...@gmail.com> wrote: >>> post some small sample input set with the expected output from that input >>> and a brief description of what you're trying to do > > sample input - posted > expected output - not posted > brief description - not posted > >> >> this is changing every time I think about it, but data file : >> >> 01 01 2012 1234.23 >> 02 01 2012 3.23 >> 02 01 2012 45.11 >> 14 01 2012 23.51 >> 22 01 2012 526.67 >> 22 01 2012 5.78 >> 02 02 2012 1.67 >> 03 02 2012 -3.69 >> >> I think I want to make one array per month ($2) that contains each day >> ($1) associated with each value $4. AFAIK at least arrays in awk can/ >> should do that, though I have considered using a more appropriate >> program. but this looks very amenable to awk. >> >> -Bryan > > Again, though, you're not telling us WHAT you're trying to do you're just > telling us HOW you think you should do it. > > I can easily say that to the solution you describe above could be implemented as: > > { arr[$2] = arr[$2] $1 " " $4 ";" } > > but I have absolutely no idea if that solution is a good way to solve your > problem or not as I still don't know what the problem actually IS that you're > trying to solve. > > If you can't at least post some expected output from the above input and a brief > description of why that's the output, then it's not worth your while posting > here as the answers you get could be totally wrong for your actual application. > > Ed. Maybe this will help. The following may or may not be applicable to your actual problem, it's just an example of something you might be trying to do to show you how to describe what you actually ARE trying to do: Description: I need to take a list of dates + amounts and produce a list per date (one per line) of the amounts associated with that date with the amounts separated by tabs. The output does not need to be sorted by date. Sample Input: 01 01 2012 1234.23 02 01 2012 3.23 02 01 2012 45.11 14 01 2012 23.51 22 01 2012 526.67 22 01 2012 5.78 02 02 2012 1.67 03 02 2012 -3.69 Expected Output: 2012 01 01 1234.23 2012 01 02 3.23 45.11 2012 01 14 23.51 2012 01 22 526.67 5.78 2012 02 02 1.67 2012 02 03 -3.69 Given the above I'd reply with something like this: $ cat tst.awk { date = $3 " " $2 " " $1 amount = $4 dt2amt[date] = dt2amt[date] "\t" amount } END { for (date in dt2amt) { print date dt2amt[date] } } $ awk -f tst.awk file 2012 02 03 -3.69 2012 01 14 23.51 2012 01 01 1234.23 2012 01 02 3.23 45.11 2012 02 02 1.67 2012 01 22 526.67 5.78 but again it all depends what you're actually trying do DO, not how you think you should do it. Ed. |
|
|||
|
Ed, the examples you wrote appear to be what is called concatenation
-- I was entirely unaware of how this works though I had heard about it. The output appears very interesting, because it has each cost associated with a year, day and date. Then I ought to be able to do a lot of math and sorting on it in various ways. I will have to work on these excellent suggestions and the other interesting recent threads. Thanks a lot for the help, I appreciate it, especially since I know my question is rather nebulous. -Bryan p.s: time for me to read Eric Raymond's "How To Ask Smart Questions" again ... |
|
|||
|
On Sunday, February 26, 2012 2:18:07 PM UTC-5, Ed Morton wrote:
> $ cat tst.awk > { > date = $3 " " $2 " " $1 > amount = $4 > dt2amt[date] = dt2amt[date] "\t" amount > } > END { > for (date in dt2amt) { > print date dt2amt[date] > } > } > > $ awk -f tst.awk file > 2012 02 03 -3.69 > 2012 01 14 23.51 > 2012 01 01 1234.23 > 2012 01 02 3.23 45.11 > 2012 02 02 1.67 > 2012 01 22 526.67 5.78 > > but again it all depends what you're actually trying do DO IIUC dt2amt[date] in this sample contains concatenated strings, not variables, and therefore arithmetic operators will not work on them. this is a consequence of awk not providing a concatenation operator as such. .... I would really like to add those numbers together e.g. on a per-day basis. -Bryan |
|
|||
|
bryanlepore@gmail.com <bryanlepore@gmail.com> wrote:
> On Sunday, February 26, 2012 2:18:07 PM UTC-5, Ed Morton wrote: > > $ cat tst.awk > > { > > date = $3 " " $2 " " $1 > > amount = $4 > > dt2amt[date] = dt2amt[date] "\t" amount > > } > > END { > > for (date in dt2amt) { > > print date dt2amt[date] > > } > > } > > > > $ awk -f tst.awk file > > 2012 02 03 -3.69 > > 2012 01 14 23.51 > > 2012 01 01 1234.23 > > 2012 01 02 3.23 45.11 > > 2012 02 02 1.67 > > 2012 01 22 526.67 5.78 > > > > but again it all depends what you're actually trying do DO > > IIUC > > dt2amt[date] in this sample contains concatenated strings, not variables, and therefore arithmetic operators will not work on them. this is a consequence of awk not providing a concatenation operator as such. Sorry I don't understand you. Arrays hold data. What would it mean for an array to hold a variable? What difference would it make to the data if you needed and explicit operator to concatenate strings? > > .... I would really like to add those numbers together e.g. on a per-day basis. Then just use add the values as numbers instead of concatenating them as strings: { date = $3 " " $2 " " $1 amount = $4 dt2amt[date] += amount } END { for (date in dt2amt) { print date, dt2amt[date] } } Regards, Ed. Posted using www.webuse.net |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|