Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How could I simplify this redundant-column-removing code?

by pme (Prior)
on Jun 17, 2015 at 16:03 UTC ( #1130837=note: print w/replies, xml ) Need Help??


in reply to How could I simplify this redundant-column-removing code?

Hi rubystallion,

This solution define F1 as hash variable. Unfortunately not much simpler if simpler at all.

#!/usr/bin/perl -w use strict; my %F1; my @recs; while (my $line = <DATA>) { chomp $line; my @F = split '&', $line; if ($. == 1) { $F1{$_} = 0 for (@F); } die "NF mismatch" if keys %F1 != @F; push @recs, \@F; foreach (@F) { $F1{$_}++ if exists $F1{$_}; } } for my $rec (@recs) { for my $field (@$rec) { print "$field\t" unless exists $F1{$field} and $F1{$field} == + $.; } print "\n"; } __DATA__ a=1&b=1&c=1&d=2&e=&f=3 a=1&b=2&c=3&d=2&e=&f=4 a=1&b=2&c=5&d=1&e=&f=5
Output:
b=1 c=1 d=2 f=3 b=2 c=3 d=2 f=4 b=2 c=5 d=1 f=5

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1130837]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2020-01-28 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?