Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Convert Tcl Keyed List to Perl

by hdb (Monsignor)
on May 22, 2013 at 20:01 UTC ( #1034812=note: print w/replies, xml ) Need Help??

in reply to Convert Tcl Keyed List to Perl

Re-using a parser I have applied on PM several times now:

use strict; use warnings; use Data::Dumper; my %tcl; my $ptr; my $threshold = 3; sub store_word { my ( $level, $word ) = @_; $word =~ s/^\s+//g; $word =~ s/\s+$//g; return "" unless $word; if( $level <= $threshold ) { $tcl{$word} = {} if not defined $tcl{$word}; $ptr = $tcl{$word}; } else { my ( $call, $value ) = split / /, $word; $ptr->{ $call } = $value; } return ""; } while(my $input = <DATA>){ my $level = 0; my $word = ""; my %action = ( '{' => sub { $word = store_word( $level++, $word + ) }, '}' => sub { $word = store_word( $level--, $word + ) }, 'default' => sub { $word .= shift }, ); ( $action{$_} // $action{'default'} )->($_) for $input =~ /./g; } print Dumper(\%tcl); print $tcl{'Make Cal l'}{'CumulativeSuccessfullyEstablished(calls)(In) +'}, "\n"; __DATA__ {{Register {{CumulativeActive(calls)(In) 0} {CumulativeSuccessfullyEst +ablished(calls)(In) 0} {CumulativeSuccessfullyCompleted(calls)(In) 0} + {CumulativeUnsuccessful(calls)(In) 0} }}} {{{Make Cal l} {{Cumulativ +eActive(calls)(In) 0} {CumulativeSuccessfullyEstablished(calls)(In) 0 +} {CumulativeSuccessfullyCompleted(calls)(In) 0} {CumulativeUnsuccess +ful(calls)(In) 0} }}} {{Initiating {{CumulativeA ctive(calls)(In) 0} +{CumulativeSuccessfullyEstablished(calls)(In) 0} {CumulativeSuccessfu +llyCompleted(calls)(In) 0} {CumulativeUnsuccessful(calls)(In) 0} }}} +{{Summary {{CumulativeActive(calls)(In) 0} {CumulativeSuccessfullyEst +ablished(calls)(In) 0} {CumulativeSuccessfullyCompleted(calls)(In) 0} + {CumulativeUnsuccessful(calls)(In) 0} }}}

Replies are listed 'Best First'.
Re^2: Convert Tcl Keyed List to Perl
by LanX (Bishop) on May 22, 2013 at 23:19 UTC
    It's irritating, you joined the elements of a list {Make Cal l} to form a hash-key.

    While this might be intended it doesn't seem to be the correct format of "keyed lists"! ³

    A keyed list is a list in which each element contains a key and value pair. These element pairs are stored as lists themselves, where the key is the first element of the list, and the value is the second. The key-value pairs are referred to as fields. This is an example of a keyed list:
    % package require Tclx 8.4 % keylset person NAME {Frank Zappa} JOB {musician and composer} % list $person {{NAME {Frank Zappa}} {JOB {musician and composer}}}
    Fields may contain subfields; `.' is the separator character. Subfields are actually fields where the value is another keyed list. Thus the following list has the top level fields ID and NAME, and subfields NAME.FIRST and NAME.LAST:
    {ID 106} {NAME {{FIRST Frank} {LAST Zappa}}}

    The difficulty to find valid information on TCL¹ plus the broken data the OP (or his TCL application) provided made me stop working on this task.²

    But I like your recursive parser, it's only usable for this special thread (hardcoded levels) , but nevertheless nicely made! =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    ¹) glad to be a Perler...

    ²) to be honest I finished a kl2hash parser but I don't intend to publish.

    ³) Never mind, it's legal string syntax in TCL. There is no big difference between lists and strings in that language.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1034812]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-06-23 11:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.