Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
toy list processing problem: collect similar terms
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Dr.Ruud, post: 4135238"] The input is a string on STDIN, and the output is a string on STDOUT? Use a hash: perl -MData::Dumper -wle '$Data::Dumper::Sortkeys = 1; my $t = "((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)" . " (2 k l) (4 m n) (2 o p) (4 q r) (5 s t))"; push @{ $h{ $1 } }, $2 while $t =~ /(\w+)([^)]*)/g; # gist print Dumper \%h; ' or an array: perl -wle ' my $t = "((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)" . " (2 k l) (4 m n) (2 o p) (4 q r) (5 s t))"; push @{$a[$1]},$2 while $t =~ /(\w+)\s+([^)]*)/g; # gist.1 print "((".join(") (",map join(" ",@$_),@a )."))"; # gist.2 ' Or if the list is not just a string, but a real data structure in the script: perl -wle' my $t = [ [qw/0 a b/], [qw/1 c d/], [qw/2 e f/], [qw/3 g h/], [qw/1 i j/], [qw/2 k l/], [qw/4 m n/], [qw/2 o p/], [qw/4 q r/], [qw/5 s t/] ]; push @{ $a[ $_->[0] ] }, [ @$_[ 1, 2 ] ] for @$t; # AoAoA printf "((%s))\n", join ") (", map join( " ", map join( " ", @$_ ), @$_ ), @a; ' Etc. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
toy list processing problem: collect similar terms
Top