in reply to finding colunm header which crossing multiple rows

#!/usr/bin/perl # https://perlmonks.org/?node_id=1225917 use strict; use warnings; $_ = <DATA>; my @titles = /\w+/g; my @positions; push @positions, $-[0] while /\w+/g; while( <DATA> ) { /\S/ or last; for my $i (0 .. $#positions) { /^.{$positions[$i]}(\w+)/ and $titles[$i] .= " $1"; } } use Data::Dump 'dd'; dd \@titles; __DATA__ Name Company Work Home + Work Name Address Phone + Hours Number

Outputs:

[ "Name", "Company Name", "Work Address", "Home Phone Number", "Work Hours", ]

Replies are listed 'Best First'.
Re^2: finding colunm header which crossing multiple rows
by ikegami (Patriarch) on Nov 16, 2018 at 21:12 UTC

    Or

    $_ = <DATA>; my @titles = split; my @lens = map length, /\S+\s*/g; $lens[-1] = '*'; my $pat = join "", map "A".$_, @lens; while (defined( $_ = <DATA> ) && /\S/) { my @more = unpack($pat, $_); for my $i (0..$#titles) { $titles[$i] .= ' '.$more[$i] if length($more[$i]); } }

    This can be reduced to the following:

    $_ = <DATA>; my @titles = split; my @lens = map length, /\S+\s*/g; $lens[-1] = '*'; my $pat = join "", map "A".$_, @lens; while (defined( $_ = <DATA> ) && /\S/) { my @more = unpack(" ".$pat, $_); $titles[$_] .= $more[$_] for 0..$#titles; }

      Thank you ikegami! I got the idea to match up string position and concatenate to the final column names

Re^2: finding colunm header which crossing multiple rows
by cscu2007 (Novice) on Nov 16, 2018 at 21:18 UTC

    This is Awesome. Thanks tybalt89!