开发者

Automatic update data file and display?

I am trying to develop a system where this application allows user to book ticket seat. I am trying to implement an automatic system(a function) where the app can choose the best seats for the user.

My current database(seats.txt) file store in this way(not sure if its a good format:

X000X
00000
0XXX0

where X means the seat is occupied, 0 means nothing.

After user login to my system, and choose the "Choose best for you", the user will be prompt to enter how many seats he/she want (I have done this part), now, if user enter: 2, I will check from first row, see if there is any empty seats, if yes, then I assign(this is a simple way, once I get this work, I will write a better "automatic-booking" algorithm)

I try to play with sed, awk, grep.. but it just cant work (I am new to bash programming, just learning bash 3 days ago).

Anyone can help?

FYI: The seats.txt format doesn't have to be that way. It can also be, store all开发者_开发知识库 seats in 1 row, like: X000X0XXX00XXX

Thanks =)


Here's something to get you started. This script reads in each seat from your file and displays if it's taken or empty, keeping track of the row and column number all the while.

#!/bin/bash

let ROW=1
let COL=1

# Read one character at a time into the variable $SEAT.
while read -n 1 SEAT; do
    # Check if $SEAT is an X, 0, or other.
    case "$SEAT" in
        # Taken.
        X)  echo "Row $ROW, col $COL is taken"
            let COL++
            ;;

        # Empty.
        0) 
            echo "Row $ROW, col $COL is EMPTY"
            let COL++
            ;;

        # Must be a new line ('\n').
        *)  let ROW++
            let COL=1
            ;;
    esac
done < seats.txt

Notice that we feed in seats.txt at the end of the script, not at the beginning. It's weird, but that's UNIX for ya. Curiously, the entire while loop behaves like one big command:

while read -n 1 SEAT; do {stuff}; done < seats.txt

The < at the end feeds in seats.txt to the loop as a whole, and specifically to the read command.


It's not really clear what help you're asking for here. "Anyone can help?" is a very broad question.

If you're asking if you're using the right tools then yes, the text processing tools (sed/awk/grep et al) are ideal for this given the initial requirement that it be done in bash in the first place. I'd personally choose a different baseline than bash but, if that's what you've decided, then your tool selection is okay.

I should mention that bash itself can do a lot of the things you'll probably be doing with the text processing tools and without the expense of starting up external processes. But, since you're using bash, I'm going to assume that performance is not your primary concern (don't get me wrong, bash will probably be fast enough for your purposes).

I would probably stick with the multi-line data representation for two reasons. The first is that simple text searches for two seats together will be easier if you keep the rows separate from each other. Otherwise, in the 5seat-by-2row XXXX00XXXX, a simplistic search would consider those two 0 seats together despite the fact they're nowhere near each other:

XXXX0
0XXXX

Secondly, some people consider the row to be very important. I won't sit in the first five rows at the local cinema simply because I have to keep moving my head to see all the action.

By way of example, you can get the front-most row with two consecutive seats with (commands are split for readability):

pax> cat seats.txt
X000X
00000
0XXX0

pax> expr $(
     (echo '00000';cat seats.txt)
         | grep -n 00
         | tail -1
         | sed 's/:.*//'
     ) - 1
2

The expr magic and extra echo are to ensure you get back 0 if no seats are available. And you can get the first position in that row with:

pax> cat seats.txt
     | grep 00
     | tail -1
     | awk '{print index($0,"00")}'
3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜