foogod has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have been racking my brain trying to figure out a way (logic) to do the following:

I have this line:

\player_0\STOMP\frags_0\0\ping_0\ 146\team_0\0\mesh_0\Male Soldier\ski +n_0\TOPModels.Skins.CCSM\face_0\TOPModels.Skins.Legs\ngsecret_ 0\false\player_1\animals\frags_1\0\ping_1\ 374\team_1\1\mesh_1\Male So +ldier\skin_1\TOPModels.Skins.SFMe\face_1\TOPModels.Skins.SWATT ex0\ngsecret_1\false\player_2\Rigodon\frags_2\0\ping_2\ 159\team_2\1\m +esh_2\Male Soldier\skin_2\TOPModels.Skins.SFMe\face_2\TOPModel s.Skins.SWATTex0\ngsecret_2\false\queryid\64.1

sorry for length. this result string is \COLUMN\VALUE

What i need to do is map/split/???? these values so that the corresponding COLUMN = VALUE per line or (even better) per variable.
$column1 = $value1;
$column2 = $value2;
etc etc etc

but alas ... I cannot even come up with a good solid logic, much less the code for it.

What I am looking for is not so much code, but that silly little function/logic that is escaping me.

Any help is greatly appreciated.

thanks,

- f o o g o d

Replies are listed 'Best First'.
Re: Split line with corresponding columns and values
by gloryhack (Deacon) on Nov 11, 2001 at 01:11 UTC
    #!/usr/bin/perl use strict; my $data = '\player_0\STOMP\frags_0\0\ping_0\ 146\team_0\0\mesh_0\Male + Soldier\skin_0\TOPModels.Skins.CCSM\face_0\TOPModels.Skins.Legs\ngse +cret_0\false\player_1\animals\frags_1\0\ping_1\ 374\team_1\1\mesh_1\M +ale Soldier\skin_1\TOPModels.Skins.SFMe\face_1\TOPModels.Skins.SWATTe +x0\ngsecret_1\false\player_2\Rigodon\frags_2\0\ping_2\ 159\team_2\1\m +esh_2\Male Soldier\skin_2\TOPModels.Skins.SFMe\face_2\TOPModels.Skins +.SWATTex0\ngsecret_2\false\queryid\64.1'; my %hash = split(/\\/, $data); foreach (keys %hash) { print "$_ = $hash{$_}\n"; } exit;
      Oops... with that leading bugslash on there, you'll want to do this after the assignment of $data:
      $data =~ s/^\\//;

      Thanks alot guys!

      For whatever reason I completely forgot about the key value ... its been a long month! :-)

      This is working great ... I am also checking into Data::Dumper ... but for now this is great.

      Thanks for the fast (very fast!) replies.

      - f o o g o d

Re: Split line with corresponding columns and values
by demerphq (Chancellor) on Nov 11, 2001 at 03:36 UTC
    I would build either a list of hashes, or a hash of hashes:
    use warnings; use strict; use Data::Dumper; my $data = '\player_0\STOMP\frags_0\0\ping_0\ 146\team_0\0\mesh_0\Male + Soldier'. '\skin_0\TOPModels.Skins.CCSM\face_0\TOPModels.Skins.Legs\n +gsecret_0\false'. '\player_1\animals\frags_1\0\ping_1\ 374\team_1\1\mesh_1\Ma +le Soldier'. '\skin_1\TOPModels.Skins.SFMe\face_1\TOPModels.Skins.SWATTe +x0'. '\ngsecret_1\false\player_2\Rigodon\frags_2\0\ping_2\ 159\t +eam_2\1'. '\mesh_2\Male Soldier\skin_2\TOPModels.Skins.SFMe\face_2\TO +PModels.Skins.SWATTex0'. '\ngsecret_2\false\queryid\64.1'; #Build a heirarchy my @list; my %tree; while ($data=~/\G\\(\w+)_(\d+)\\([^\\]+)/g) { $list[$2]->{$1}=$3; $tree{$2}->{$1}=$3; } print Dumper(\@list,\%tree); __END__ $VAR1 = [ { 'face' => 'TOPModels.Skins.Legs', 'skin' => 'TOPModels.Skins.CCSM', 'frags' => '0', 'team' => '0', 'ping' => ' 146', 'player' => 'STOMP', 'ngsecret' => 'false', 'mesh' => 'Male Soldier' }, { 'face' => 'TOPModels.Skins.SWATTex0', 'skin' => 'TOPModels.Skins.SFMe', 'frags' => '0', 'team' => '1', 'ping' => ' 374', 'player' => 'animals', 'ngsecret' => 'false', 'mesh' => 'Male Soldier' }, { 'face' => 'TOPModels.Skins.SWATTex0', 'skin' => 'TOPModels.Skins.SFMe', 'frags' => '0', 'team' => '1', 'ping' => ' 159', 'player' => 'Rigodon', 'ngsecret' => 'false', 'mesh' => 'Male Soldier' } ]; $VAR2 = { '0' => { 'face' => 'TOPModels.Skins.Legs', 'skin' => 'TOPModels.Skins.CCSM', 'frags' => '0', 'team' => '0', 'ping' => ' 146', 'player' => 'STOMP', 'ngsecret' => 'false', 'mesh' => 'Male Soldier' }, '1' => { 'face' => 'TOPModels.Skins.SWATTex0', 'skin' => 'TOPModels.Skins.SFMe', 'frags' => '0', 'team' => '1', 'ping' => ' 374', 'player' => 'animals', 'ngsecret' => 'false', 'mesh' => 'Male Soldier' }, '2' => { 'face' => 'TOPModels.Skins.SWATTex0', 'skin' => 'TOPModels.Skins.SFMe', 'frags' => '0', 'team' => '1', 'ping' => ' 159', 'player' => 'Rigodon', 'ngsecret' => 'false', 'mesh' => 'Male Soldier' } };
    HTH

    Yves / DeMerphq
    --
    Have you registered your Name Space?

Re: Split line with corresponding columns and values
by princepawn (Parson) on Nov 11, 2001 at 01:11 UTC
    open D, 'datafile' or die $!; my $data = join '', <D>; my %data = split /\\/, $data; warn $data{frags_0};

    But: the second I see code with variables named with a noun fused with an adjected (e.g., frags_0 then I think there must be a better way to represent the data, such as with Data::Dumper.

Re: Split line with corresponding columns and values
by osfameron (Hermit) on Nov 11, 2001 at 01:17 UTC
    Just assign the split data to a hash:
    my %hash=split /\\/, $line; print $hash{mesh_0};
    That won't quite work with the above line, because it has a prepending '\' so your keys and values are out of sync, but it's a start...

    If you really want the variable named like $player_0, $mesh_0 etc., then you can mess around with symbolic references (but only if you know why they are bad...) I think that a hash ought to do fine though!

    Cheerio!
    Osfameron

Re: Split line with corresponding columns and values
by educated_foo (Vicar) on Nov 11, 2001 at 01:12 UTC
    my %values = split /\\/, $string;
    
    Then
    $values{player_0} eq 'STOMP'
    etc.