Sorry, but this is a newbie question. I have a CGI that takes in five user inputs and saves that to a delimited text file that logs all such input. The script then pulls all of the data back out of the file and sorts the data. The data is brought back into the script and stored in separate arrays for the individual elements. In other words, the first data element is "Date" in the form "20010712". The second array is "Provider", the third is "Description", etc.
I want to sort the "Date" array, but I also want the other elements of the other arrays to be sorted consistent with the changes made to the "Date" array. The only way I know how to do this is with a bubble sort. I wrote the following, which ain't workin' so good. Can anyone point me in the right direction? I know how to do this in Basic using line numbers, but I can't get it done in Perl.
__________________________________________________
# get total number of data lines entered
# by using the "date" array
$totalentries = $#date;
# move it up one to make the count accurate
$totalentries = $totalentries + 1;
# Bubble Sort Data
# set a Flag
$flag = 1;
until ($flag == 0) {
$n = 0;
$counter = $totalentries;
$flag = 0;
# the flag is changed from 1 to 0.
# Not sure if the "until" function will drop out
# at this point or not. Don't know a better way. . .
while ($counter > 0) {
$n2 = $n + 1;
if (int $date[$n] < int $date[$n2]) {
# if this is true, perform the swap of
# the same elements in each array
$placeholder = $date[$n];
$date[$n] = $date[$n2];
$date[$n2] = $placeholder;
$placeholder = $provider[$n];
$provider[$n] = $provider [$n2];
$provider[$n2] = $placeholder;
$placeholder = $description[$n];
$description[$n] = $description[$n2];
$description[$n2] = $placeholder;
$placeholder = $exhibit[$n];
$exhibit[$n] = $exhibit[$n2];
$exhibit[$n2] = $placeholder;
$placeholder = $page[$n];
$page[$n] = $page[$n2];
$page[$n2] = $placeholder;
# if a change occurred, change the flag to "1"
$flag = 1;
}
++$n;
--$counter;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.