Solved

png header

Posted on 2002-04-10
7
122 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now