|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Learn four approaches for automating Excel logic, along with advantages and disadvantages of each. Read all about it in the free whitepaper: “Tapping into Excel Logic from Java: Four Server-Based Alternatives” Download Now! |
|
#1
|
|||
|
|||
|
Grappling with arrays...
I have a mental block trying to work with arrays and I was hoping someone could explain/show me how it should be done.
I wrote a script (just to train myself) which created a flat file tab delimited db and felt rather smug. However the next part has caused me really to wrestle with connecting identities to the contents of an array. I'll show you what I did, (I got the job done, at least!) and then pose some questions: Code:
open (DATA, "$text1dat") || die "Couldn't get data page"; @all=<DATA>; close (DATA); @allnew = split (/\t/, $all[0]); $sitename = "$allnew[0]"; $pagename = "$allnew[1]"; $text1 = "$allnew[2]"; $text2 = "$allnew[3]"; $text3 = "$allnew[4]"; $text4 = "$allnew[5]"; It was a real struggle to get to that (and I'm pleased because I now understand the fundamentals a lot better), and it did the job but it doesn't seem to be very 'perl' - just basic and cumbersome. How can I express it so that the scalars match up with the correct elements of the array in more succinct fashion (with explanation, please! )Also, is it possible to make the lists open ended so that the number of elements and scalars expands to the requirements of the moment? Eg in one use there might be two elements, another a dozen, which would need to be tagged with a scalar. If so how might it be done? Thanks very much |
|
#2
|
||||
|
||||
|
TMTOWTDI (tim-t-ow-dee) - There's More Than One Way To Do It
Code:
open (DATA, "$text1dat") || die "Couldn't get data page";
@all=<DATA>;
close (DATA);
while($line = shift @all) {
($sitename, $pagename, $text1, $text2, $text3, $text4) = split(/\t/, $line);
}
Code:
open (DATA, "$text1dat") || die "Couldn't get data page";
@all=<DATA>;
close (DATA);
foreach $line(@all) {
($sitename, $pagename, $text1, $text2, $text3, $text4) = split(/\t/, $line);
}
There's lots of ways to do it! In the first example, I use the shift function to remove the first element of the array ([0]) and return it to $line. That shortens the array by 1 (that is, [0] goes into $line and [1] becomes the new [0], [2] becomes the new [1], etc.). If you know, the number of elements that are going to be coming out of the line, you can assign them directly to scalars. The split operation works left to right on $line, so whatever the first "field" of $line is goes into $sitename, the second field goes to $pagename, etc. Then, I go back up to the while loop again and shift @all another time, thus repeating the process. When @ all has a 0 length, shift returns a false value (I believe, it actually returns the undef value) and the loop ends. This method, of course, does NOT preserve @all as it will eventually get emptied. The second method, however, does preserve @all. foreach $line(@all) does exactly what it sounds like: foreach $line in @all it does the stuff in the block (curly braces). The stuff in the block is exactly the same as above. The only difference here is that when foreach() is done the array remains unchanged so you can use it again if you want. Other array functions you may want to read about in the perlfunc manpage (or, HTML page if you use Win32 Activestate): splice, pop, push, unshift - all relatives of shift in some way sort, reverse, join More advanced list stuff: grep, map, unpack, pack Incidentally: "$allnew[x]" <- the quotes are not "magical", that is, they don't do anything. You don't need them in this context. Have fun ![]() |
|
#3
|
|||
|
|||
|
Wow - there's enough there for a month! Thanks a lot, Ctb.
I had looked at the 'for each' option but was getting it totally wrong. Instead of creating a scalar ($line), using it then dumping it (you callous brute! ) I worried about defining it and what would happen to it afterwards.I feel like a weight has been lifted... |
![]() |
| Viewing: Dev Shed Forums > Other > Beginner Programming > Grappling with arrays... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|