Solved

png header

Posted on 2002-04-10
7
128 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

840 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