Arengin has asked for the wisdom of the Perl Monks concerning the following question:
With the input file like#!/usr/bin/perl use strict; use warnings; my %data; my $cur_tag; open (FILE, '<', $ARGV[0]) or die "Could not open file: $!"; while (<FILE>) { chomp; if ( my ($tag,$line) = /^(X\S+)\s+(.+)$/ ) { $data{$tag} .= "\n" if exists $data{$tag}; $data{$tag} .= $line; $cur_tag = $tag; } elsif ($cur_tag && !/^BEGIN_TAG/) { $data{$cur_tag} .= " NEWLINE ".$_; } } for my $tag (sort keys %data) { print "$tag: $data{$tag}\n"; }
And the returnBEGIN_TAG X1 test test test X2 no no no no X3 yes yes yes BEGIN_TAG X1 test test test tes test test X2 no no no no no nono non no no nononono no no no X3 hi hi hi hi hi hi hi hi hi hi hi hi
Meaning all X1, all X2 and than all X3X1: test test test test test test NEWLINE tes test test X2: no no no no no no no no no NEWLINE nono non no no NEWLINE nononono no no no X3: yes yes yes hi hi hi hi hi hi NEWLINE hi hi hi hi NEWLINE hi hi
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Returning an array in a specific way for csv
by hippo (Archbishop) on Mar 13, 2017 at 16:01 UTC | |
|
Re: Returning an array in a specific way for csv
by haukex (Archbishop) on Mar 13, 2017 at 15:54 UTC | |
by Arengin (Novice) on Mar 13, 2017 at 16:03 UTC | |
by haukex (Archbishop) on Mar 13, 2017 at 16:18 UTC | |
by Arengin (Novice) on Mar 14, 2017 at 08:25 UTC | |
|
Re: Returning an array in a specific way for csv
by tybalt89 (Monsignor) on Mar 13, 2017 at 21:19 UTC | |
by Arengin (Novice) on Mar 14, 2017 at 08:28 UTC |