Solved

png header

Posted on 2002-04-10
7
127 Views
Last Modified: 2010-03-05
I have string

[Event "ASIAN CHESS"]
[Site "KOLKATA"]
[Date "????.??.??"]
[Round "?"]
[White "ISAEV, JAMSHED."]
[Black "PENG, XIAOMIN."]
[Result "1/2-1/2"]
[ECO "C65"]
[WhiteElo "2454"]
[BlackElo "2629"]
[Annotator "02"]
[PlyCount "90"]
[EventDate "2001.08.09"]

1. e4 e5 2. Nf3 Nc6 3. Bb5 Nf6 4. d4 exd4 5. O-O Be7 6. Re1 O-O 7. e5 Ne8 8.
Bf4 f6 9. Bxc6 dxc6 10. Qxd4 Qxd4 11. Nxd4 Bc5 12. c3 fxe5 13. Bxe5 Nd6 14. Nd2
Bd7 15. Ne4 Nxe4 16. Rxe4 Bb6 17. Bg3 Rae8 18. Rae1 Rxe4 19. Rxe4 c5 20. Nf3
Bc6 21. Re7 Rd8 22. h3 Bxf3 23. gxf3 Rd2 24. Be5 Kf8 25. Rxg7 Re2 26. Rg5 c4 ...

now I need to take the pares '[key "value"]' and I wont to have a hash table with all that values.

thanks
0
Comment
Question by:oleber
  • 4
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 6932925
I am assuming that you only want to hash the details, and not the numbered list of chess moves.

while ( <DATA> )
{
     $_ = /\^[(\w+)\s+"(.*)"\]$/;
     $key = $1;
     $value = $2;
     $details{$key} = $value;
}
foreach $key ( keys %details )
{
        print("KEY: $key\t\t VALUE: $details{$key}\n");
}


__DATA__
[Event "ASIAN CHESS"]
[Site "KOLKATA"]
[Date "????.??.??"]
[Round "?"]
[White "ISAEV, JAMSHED."]
[Black "PENG, XIAOMIN."]
[Result "1/2-1/2"]
[ECO "C65"]
[WhiteElo "2454"]
[BlackElo "2629"]
[Annotator "02"]
[PlyCount "90"]
[EventDate "2001.08.09"]
0
 
LVL 10

Author Comment

by:oleber
ID: 6933325
My question is if came two header lines in just one line.

let's say

[Event "ASIAN CHESS"]
[Site "KOLKATA"]
[Date "????.??.??"][Round "?"]
[White "ISAEV, JAMSHED."]
[Black "PENG, XIAOMIN."]
[Result "1/2-1/2"]
[ECO "C65"]
[WhiteElo "2454"][BlackElo "2629"]
[Annotator "02"]
[PlyCount "90"]
[EventDate "2001.08.09"]

I would like something more generic, to use in other examples similar whith this one to.
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 6935542
This should work, assuming this is the final definition for your data, you need to define it very clearly.

while ( <DATA> )
{
     chomp $_;
     $one_line = $_;

     if ( $one_line =~ /^(\[\w.*\])(\[\w.*\])$/ )
     {
           @pairs = ($1,$2);
     }
     else
     {
          @pairs = ($one_line);
     }
     
     foreach $pair ( @pairs )
     {
          $pair =~ /^\[(\w+)\s+"(.*)"\]$/;
          $key = $1;
          $value = $2;
          $details{$key} = $value;
     }
}
foreach $key ( sort keys %details )
{
     print("KEY: $key\t\t VALUE: $details{$key}\n");
}


__DATA__
[Event "ASIAN CHESS"]
[Site "KOLKATA"]
[Date "????.??.??"][Round "?"]
[White "ISAEV, JAMSHED."]
[Black "PENG, XIAOMIN."]
[Result "1/2-1/2"]
[ECO "C65"]
[WhiteElo "2454"][BlackElo "2629"]
[Annotator "02"]
[PlyCount "90"]
[EventDate "2001.08.09"]
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 19

Accepted Solution

by:
Kim Ryan earned 50 total points
ID: 6941296
OK, I worked out a slightly better way of handling this:

@orig_lines = <DATA>;
foreach $line ( @orig_lines )
{
     chomp $line;
     if ( $line =~ /^(\[\w.*\])(\[\w.*\])$/ )
     {
          push(@new_lines,$1,$2);    
     }
     else
     {
          push(@new_lines,$line);    
     }
}

foreach  ( @new_lines )
{
     /^\[(\w+)\s+"(.*)"\]$/;
     $key = $1;
     $value = $2;
     $details{$key} = $value;
}
foreach $key ( sort keys %details )
{
     print("KEY: $key\t\t VALUE: $details{$key}\n");
}


__DATA__
[Event "ASIAN CHESS"]
[Site "KOLKATA"]
[Date "????.??.??"][Round "?"]
[White "ISAEV, JAMSHED."]
[Black "PENG, XIAOMIN."]
[Result "1/2-1/2"]
[ECO "C65"]
[WhiteElo "2454"][BlackElo "2629"]
[Annotator "02"]
[PlyCount "90"]
[EventDate "2001.08.09"]
0
 
LVL 10

Author Comment

by:oleber
ID: 6941430
I sead generic, In generic I was sinking in someting like

[Event "ASIAN CHESS"]
[Site "KOLKATA"]
[Date "????.??.??"][Round "?"]
[White "ISAEV, JAMSHED."]
[Black "PENG, XIAOMIN."]
[Result "1/2-1/2"]
[ECO "C65"]
[WhiteElo "2454"][BlackElo "2629"]
[Annotator "02"]
[PlyCount "90"]
[EventDate "2001.08.09"]


Or

[Event "ASIAN CHESS"]
[Site "KOLKATA"]
[Date "????.??.??"][Round "?"][White "ISAEV, JAMSHED."][Black "PENG, XIAOMIN."][Result "1/2-1/2"][ECO "C65"]
[WhiteElo "2454"][BlackElo "2629"]
[Annotator "02"]
[PlyCount "90"]
[EventDate "2001.08.09"]

or

[Event "ASIAN CHESS"][Site "KOLKATA"][Date "????.??.??"][Round "?"][White "ISAEV, JAMSHED."][Black "PENG, XIAOMIN."][Result "1/2-1/2"][ECO "C65"][WhiteElo "2454"][BlackElo "2629"][Annotator "02"][PlyCount "90"][EventDate "2001.08.09"]

0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 6943355
Do you know the maximum number of pairs per line? If you like I can work out an answer based on this, if you still need it.
0
 
LVL 10

Author Comment

by:oleber
ID: 6944370
There is no need, my problem is not with this especific problem.

Some times I need to do things simples and I do allwais this streng code. It seems to me that shoud exists a more easy way to do it.

Thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question