Date: Sun, 09 Sep 2001 20:06:43 GMT From: Sudhir Krishnan Subject: Re: Arrays, References and indexes. Message-Id: <3B9BCC9B.3C2AC609@newmail.net> This is a multi-part message in MIME format. --------------02F1F10F963F2A424F3F6C7F Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Thanks Martien! Actually I've got the code working as I want. It's just that since I'm new to perl, I think my code is Cish. I was just wanting to know if I can reduce the lines the code and make it more perlish! I'm just attaching 2 sample data files, just in case you're interested in knowing more about the problem. I'm finding it a little difficult to explain the problem in a simple way without getting into too much detail!! Anyway, here goes. This is just FYI. Actually I simplified the problem a bit, when I posted. The way the data is defined, the data structure is 4D. The way to read the data files. p1 and p2 first 2 lines: result header next 3 lines: cgi header next 3 lines: system wide stanza (another header)
3rd and 4th last lines: physical system trailer 2nd last and last lines: file trailer. ####################################################3 The body consists of 'stanzas' Each stanza starts with "*FC ????????" So all text, starting with *FC until the next *FC is a stanza. I process each file convert stanza to line format for simplicity. i.e into something line: *FC=????????&data1=value&data2=value .... Stanzas in the file are grouped together as follows: System Stanza Subsystem Stanza Subsystem Stanza . . . System Stanza subsystem stanza subsystem stanza . . So each stanza with its subsystem stanzas is what I called a 'section' The problem is to come up with one ouput file, that'll contain all the system stanzas from all files. Also all subsystem stanzas should be present under them. Corresponding system stanzas can have duplicate sub-system stanza entries, duplicates should be removed After I process each file, it'll look something like: FC=????????&*VC=5.0&*TM=1&*YL=U1.17&*SE=1&*VK=& ss[0][0] ss[0][1] ss[0][2] ss[0][3] ss[0][4] ss[0][5] *FC=????????&*VC=5.0&*TM=2&*YL=U1.17&*SE=2&*VK=& ss[1][0] ss[1][1] ss[1][2] ss[1][3] ss[1][4] ss[1][5] Imagine a second file thats the same as above, but with ss[a][b] instead of ss[0][4] Then the output file will be the same as the input files except that the first system stanza except there will be an additional entry ss[a][b] in the first section. Do you think this problem is worthy of perl? thanks Sudhir > > On Sun, 09 Sep 2001 11:59:50 GMT, > Sudhir Krishnan wrote: > > > i=file index > > j=section index in file > > How do you distinguish between the 'sections'? > > > k=line (record) index in (file, section) > > > While iterating over the files, I'm storing it as: > > > > a[i][j][k] = $_; > > > > And it seems to work fine. > > > > Problems: > > 1) I don't really understand the underlying implementation > > with references. > > Have you read the documentation, specifically perlref, perllol and > perldsc? They go into quite some detail about how perl references, and > perl's complex data structures work.. > > > 2) I use a[i][j][k] for getting/setting values, its > > 3 loops and is cumbersume. > > You will, most likely need three loops. However, you may not need them > all explicitly. You could do something like: > > Read the line into a (lexically scoped) array, splitting it. Then use an > assignment like: > > $a[i][j] = \@array; > > or, if the array needs to be reused, store a reference to a copy of the > array: > > $a[i][j] = [@array]; > > Of course, you can do the same thing with the files, one level up. > You'll still be using three iterations, just not as specific anymore, > maybe. > > > 3) I'd like to know how I can get/set values without using indexes. > > > > i.e for a simple array: > > instead of > > for ($i=0; $i <=$#ARR; $i++) > > > > I'd like to use > > for $val (@ARR) > > > > But in my case, $val will be a reference until I read the 3rd level, > > right? > > I'm not entirely sure what you mean. $val, in this for loop, will always > specifically be an alias (not the same as a reference, but similar) to > the things you're iterating over. If there isn't anything yet to be an > alias to, then how can this work? I mean, you're filling the data > structure, right? So the data and the references aren't there yet... > > If these things that you work with are all numbered, what's wrong with > using indexes? If you'd rather use push to add new data to the end, you > could do something like (untested pseudo-code follows): > > my @files; > > foreach my $file (@files) > { > my @sections; # all the sections in a file > my @section; # the records in a single section in a file > > open my $fh, $file or die $!; > while (<$fh>) > { > if (line_looks_like_new_section) > { > push @sections, [@section]; > @section = (); > } > else > { > my @records = split; > push @section, \@records; > } > } > > push @array, \@sections; > } > > ... unless I've misunderstood your structure. If I have, I'm sure you'll > be able to get the idea and adapt it to your needs. > as you can see, you can just store a reference to @sections and > @records, since they will go out of scope immediately after you've > stored them, and therefore won't be reused. @section, however, stays in > scope, and therefore, you need to make a copy of the array, and store a > reference to that, and empty the array after that. > > Martien > > PS. If you provide some real data next time, not much, but just enough, > I might be tempted to write some real code. > -- > Martien Verbruggen | Since light travels faster than > Interactive Media Division | sound, isn't that why some people > Commercial Dynamics Pty. Ltd. | appear bright until you hear them > NSW, Australia | speak? --------------02F1F10F963F2A424F3F6C7F Content-Type: text/plain; charset=UTF-8; name="p1" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="p1" RESULT=0 1 osimage discord.austin.ibm.com *VC 15.0 *N1 1.3.0.0 *DC Tue Jan 16 14:42:42 CST 2001 *FC ???????? *VC 5.0 *TM 7017-S90 *SE 8386522 *PI 123456789 *OS 4.3.0.0 *FC ???????? *VC 5.0 *TM 7017-S90 *YL U1.17 *SE 8386522 *VK *FC ???????? *VC 15.0 *N1 1.3.0.0 *OS 4.3.0.0 *Z1 ssp.basic 3.2.0.0 *Z2 ssp.css 3.2.0.0 *VD discord.austin.ibm.com:001:partition one *DC Tue Jan 16 14:42:42 CST 2001 *FC ???????? *VC 5.0 *FB @@@@@@@@ *PN 00000000 *PL 00-00 *AX sys0 *FC ???????? *DS RS M CEC OP PAN *YL U1.17-L1 *SN YL1620012285 *EC F73731 *CC 248B *FN 24L1089 *DC BD 199911221000 *VK RS6K *FC ???????? *DS Memory Card *YL U1.17-TEST1 *FN 4A85 *EC 0000 *PN 0000023L7544 *SN 09351056 *MN 0016 *SZ 0256 *VK RS6K *FC ???????? *DS 2.2 GB 16 Bit SCSI Disk Drive *AX hdisk0 *PL 10-60-00-8,0 *MF IBMRISC *TM DFHSS2W *PN 74G6984 *RL 34323432 *SN 00BF3943 *EC 486509 *FN 27H0262 *Z0 000002029F00003E *Z1 RAMST04B *Z2 0068 *Z3 97092 *Z4 0002 *Z5 22 *Z6 484547 *YL U1.17-TEST2 *FC ???????? *FB @@@@@@@@ *PN 00000000 *PL 00-00 *AX sys0 *FC ???????? *VC 5.0 *TM 7088-888 *YL U1.18 *SE 98765432 *VK *FC ???????? *VC 15.0 *N1 1.3.0.0 *OS 4.3.0.0 *VD discord.austin.ibm.com:001:partition one *DC Tue Jan 16 14:42:42 CST 2001 *FC ???????? *VC 5.0 *DS IBM Air Mover *YL U1.18-F1 *FN 07H5349 *VK RS6K *FC ???????? *DS IBM Air Mover *YL U1.18-F2 *FN 07H5349 *VK RS6K *FC ???????? *DS IBM Air Mover *YL U1.18-F3 *FN 07H5349 *VK RS6K *FC ???????? *DS IBM Air Mover *YL U1.18-F4 *FN 07H5349 *VK RS6K *FC ???????? *DS 8-32W BACKPLANE *YL U1.18-P1 *SN YL100LAB0000 *PN 23L2829 *CC 25C0 *FN 11P3046 *VK RS6K *FC ???????? *DS 8-W 1.3GHZ MCM *YL U1.18-P1-C1 *SN YL1108888888 *PN 7777777 *CC 25D3 *FN 04N5555 *MD 0000 *VK RS6K *FC ???????? *DS L3 VPD/ERROR LOG *YL U1.18-P1-C10 *SN YL1108388888 *PN 7777777 *CC 25D9 *FN 04N5555 *SZ 32 *EC *BH *MD 0000 *VK RS6K *FC ???????? *DS L3 VPD/ERROR LOG *YL U1.18-P1-C11 *SN YL1108288888 *FC ???????? *VC 5.0 *TM 7040-618 *YL U0.4 *SE 987654321 *VK *FC ???????? *VC 15.0 *N1 1.3.0.0 *OS 4.3.0.0 *VD discord.austin.ibm.com:001:partition one *DC Tue Jan 16 14:42:42 CST 2001 *FC ???????? *VC 5.0 *FB @@@@@@@@ *PN 00000000 *PL 00-00 *AX sys0 *FC ???????? *DS Wide/Fast-20 SCSI I/O Controller *AX scsi4 *FN 97H7610 *YL U0.4-P1-I7-Z1-A2 *FC ???????? *DS 16Bit SCSI Disk Drive *AX hdisk1 *PL 30-68-00-8,0 *MF IBM *TM DGHS09U *FN 59H6926 *RL 30334530 *SN 68206A52 *EC E31898 *PN 59H6816 *Z0 0003029F00013A *Z1 GAGSPR603E *Z2 09RI *Z3 99022 *Z4 0001 *Z5 22 *Z6 E31777 *YL U0.4-P1-I13-Z1-A8 ==================== 0 ==================== 0 --------------02F1F10F963F2A424F3F6C7F Content-Type: text/plain; charset=UTF-8; name="p2" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="p2" RESULT=0 1 osimage jeana.austin.ibm.com *VC 15.0 *N1 1.3.0.0 *DC Tue Jan 16 14:42:42 CST 2001 *FC ???????? *VC 5.0 *TM 7017-S90 *SE 8386522 *PI 123456789 *OS 4.3.0.0 *FC ???????? *VC 5.0 *TM 7017-S90 *YL U1.17 *SE 8386522 *VK *FC ???????? *VC 15.0 *N1 1.3.0.0 *OS 4.3.0.0 *Z1 ssp.basic 3.2.0.0 *Z2 ssp.css 3.2.0.0 *VD jeana.austin.ibm.com:2:partition two *DC Tue Jan 16 14:42:42 CST 2001 *FC ???????? *VC 5.0 *FB @@@@@@@@ *PN 00000000 *PL 00-00 *AX sys0 *FC ???????? *DS RS M CEC OP PAN *YL U1.17-L1 *SN YL1620012285 *EC F73731 *CC 248B *FN 24L1089 *DC BD 199911221000 *VK RS6K *FC ???????? *DS Memory Card *YL U1.17-TEST1 *FN 4A85 *EC 0000 *PN 0000023L7544 *SN 09351056 *MN 0016 *SZ 0256 *VK RS6K *FC ???????? *DS 2.2 GB 16 Bit SCSI Disk Drive *AX hdisk0 *PL 10-60-00-8,0 *MF IBMRISC *TM DFHSS2W *PN 74G6984 *RL 34323432 *SN 00BF3943 *EC 486509 *FN 27H0262 *Z0 000002029F00003E *Z1 RAMST04B *Z2 0068 *Z3 97092 *Z4 0002 *Z5 22 *Z6 484547 *YL U1.17-TEST2 *FC ???????? *FB @@@@@@@@ *PN 00000000 *PL 00-00 *AX sys0 *FC ???????? *VC 5.0 *TM 7088-888 *YL U1.18 *SE 98765432 *VK *FC ???????? *VC 15.0 *N1 1.3.0.0 *OS 4.3.0.0 *VD jeana.austin.ibm.com:2:partition two *DC Tue Jan 16 14:42:42 CST 2001 *FC ???????? *VC 5.0 *DS IBM Air Mover *YL U1.18-F1 *FN 07H5349 *VK RS6K *FC ???????? *DS IBM Air Mover *YL U1.18-F2 *FN 07H5349 *VK RS6K *FC ???????? *DS IBM Air Mover *YL U1.18-F3 *FN 07H5349 *VK RS6K *FC ???????? *DS IBM Air Mover *YL U1.18-F4 *FN 07H5349 *VK RS6K *FC ???????? *DS 8-32W BACKPLANE *YL U1.18-P1 *SN YL100LAB0000 *PN 23L2829 *CC 25C0 *FN 11P3046 *VK RS6K *FC ???????? *DS 8-W 1.3GHZ MCM *YL U1.18-P1-C1 *SN YL1108888888 *PN 7777777 *CC 25D3 *FN 04N5555 *MD 0000 *VK RS6K *FC ???????? *DS L3 VPD/ERROR LOG *YL U1.18-P1-C10 *SN YL1108388888 *PN 7777777 *CC 25D9 *FN 04N5555 *SZ 32 *EC *BH *MD 0000 *VK RS6K *FC ???????? *DS L3 VPD/ERROR LOG *YL U1.18-P1-C11 *SN YL1108288888 *FC ???????? *VC 5.0 *TM 7040-618 *YL U0.4 *SE 987654321 *VK *FC ???????? *VC 15.0 *N1 1.3.0.0 *OS 4.3.0.0 *VD jeana.austin.ibm.com:2:partition two *DC Tue Jan 16 14:42:42 CST 2001 *FC ???????? *VC 5.0 *FB @@@@@@@@ *PN 00000000 *PL 00-00 *AX sys0 *FC ???????? *DS Wide/Fast-20 SCSI I/O Controller *AX scsi4 *FN 97H7610 *YL U0.4-P1-I7-Z1-A2-par2 *FC ???????? *DS 16Bit SCSI Disk Drive *AX hdisk1 *PL 30-68-00-8,0 *MF IBM *TM DGHS09U *FN 59H6926 *RL 30334530 *SN 68206A52 *EC E31898 *PN 59H6816 *Z0 0003029F00013A *Z1 GAGSPR603E *Z2 09RI *Z3 99022 *Z4 0001 *Z5 22 *Z6 E31777 *YL U0.4-P1-I13-Z1-A8-par2 ==================== 0 ==================== 0 --------------02F1F10F963F2A424F3F6C7F--