Hi
I was wondering how to alternate the return of an array.
As from another question I have the following code:
#!/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";
}
With the input file like
BEGIN_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
And the return
X1: 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
Meaning all X1, all X2 and than all X3
However now I was wondering how I can change that to get
First X1, first X2, first X3
second X1, second X2, second X3
like
X1;X2;X3
X1;X2;X3
Any ideas? I tried to play around with the sort of the data array but that didn't do it at all.
Thanks for any ideas.
Arengin
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.