Solved

png header

Posted on 2002-04-10
7
131 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

627 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