Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using map / Separating results 1

Status
Not open for further replies.

Alphabin

Programmer
Dec 8, 2002
119
CA
Hi,

My first thread was not very clear so I decided to create another one.

My objective is that the following fields (stored in flat file) would be displayed by category. The category would be the state which is the field [3].

-> Flat File Structure:

Code:
John|Lopes|Student|OH
Brian|Palmer|Student|CA
John|Peters|n/a|OH
John|Patterson|nothing|CA

-> Would be displayed like the following:

Code:
[b]State: [i]CA[/i][/b]
|-----------+-------------+-----------|
|Brian      | Palmer      | Student   |
|John       | Patterson   | nothing   |
|-----------+-------------+-----------|

[b]State: [i]OH[/i][/b]
|-----------+-------------+-----------|
|John       | Lopes       | Student   |
|John       | Peters      | n/a       |
|-----------+-------------+-----------|


Here's the coding:
Code:
if ($input{'st'}) {

    # Record fields.
    # Field 0 = First Name -> $fields[0]
    # Field 1 = Second Name -> $fields[1]
    # Field 2 = Status -> $fields[2]
    # Field 3 = State -> $fields[3]
    
    # Open DB file
    open(FILE,"Database.txt");
    my @database=<FILE>;
    close(FILE);
    
    # Build list
    foreach my $record (@database) {
    chop $record;

    # Split fields 
    my @fields=split(/\|/,$record);
    
    # Unless all states
    unless ($input{'st'} eq &quot;all&quot;) {
    
    # Narrow list by state if provided.
    next if ($input{'st'} != $fields[12]); 
    # Delete records not matching state
    
    }
    
    if ($input{'name'})  {
    
    # Inputted name
    if ($input{'name'} eq $fields[0]) {
    push (@found_set,[@fields]) ;
    ++$found_records;
    }
        
    }
    
}


&output;
}

It's the output section that I'm not able to produce correctly. Right now I'm able to display all results, but not separated by state. I think using map or grep would maybe the solution.

THANK YOU VERY MUCH IN ADVANCE.
WOULD BE REALLY APPRECIATED.
 
Your output function will need to iterate through a sorted array (by state) and print a new header whenever the state changes.

Something like:
Code:
my $state = &quot;&quot;;

foreach my $row (sort { $a->[12] cmp $b->[12] } @found_set) {
  # check if we've printed a heading for the state we're looking at now
  if ($row->[12] ne $state) {
    # print the heading and note that we did so
    print &quot;State: &quot; . $row->[12] . &quot;\n&quot;;
    $state = $row->[12];
  }

  # print each of the fields, comma separated
  print join(&quot;, &quot;, @{$row}) . &quot;\n&quot;;
}

That won't display the box around fields, but I think it's basically what you're looking for.
 
Thank you very much rosenk. It's really appreciated.

Is there a way I can divide the ++$found_records so that it's display the number of results for each state instead of the total number of results for all states.
 
Sure. If you made a hash, instead ([tt]%found_records[/tt]), you could just do
Code:
$found_records{$fields[12]}++;

Then the number of records found for MA would be [tt]$found_records{'MA'}[/tt].
 
Thank you again rosenk

I don't want to use too much of your time, but I wanted to know if it's possible to sort alphabetically the fields in each state. So basically sort [a] cmp for fields[0] for each state ?

Thank you again.
 
That's a bit more complicated. You can change the sort so that the keys are sorted in whatever order you want, as long as the state is the primary sort criteria. The rows within the state would be printed in whatever additional sort criteria followed state.

Something like:
Code:
foreach my $row (sort { if ($a->[12] eq $b->[12]) {
  $a->[0] cmp $b->[0]
} else {
  $a->[12] cmp $b->[12]
} } @found_set) {

That sorts saying, if the state fields are the same, then sort ascending alphabetically on the first field. If they're not the same, sort on the state.
 
Thank you again rosenk. You're really helpful and it's really appreciated

Works perfectly.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top