Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 133
  • Last Modified:

png header

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
oleber
Asked:
oleber
  • 4
  • 3
1 Solution
 
Kim RyanIT ConsultantCommented:
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
 
oleberAuthor Commented:
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
 
Kim RyanIT ConsultantCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Kim RyanIT ConsultantCommented:
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
 
oleberAuthor Commented:
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
 
Kim RyanIT ConsultantCommented:
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
 
oleberAuthor Commented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now