Dear Monks, I have a problem. This script runs:
use 5.010; use strict; use warnings; my $Year = "2011"; my $Month = "04"; my $address = "D:/myfolder/".$Year."/".$Month."/data/"; open(SOURCE, $address."TTT.txt") || die "$!"; open(AAA, ">D:/myfolder/".$Year."/".$Month."/AAA/AAA.txt") || die "$!" +; open(BBB, ">D:/myfolder/".$Year."/".$Month."/BBB/BBB.txt") || die "$!" +; open(CCC, ">D:/myfolder/".$Year."/".$Month."/CCC/CCC.txt") || die "$!" +; open(DDD, ">D:/myfolder/".$Year."/".$Month."/DDD/DDD.txt") || die "$!" +; my @AAA; my @BBB; my @CCC; my @DDD; my @contents=<SOURCE>; close (SOURCE); my $Header = shift @contents; foreach(@contents) { when (/;AA/) {push @AAA, $_;} when (/;BB/) {push @BBB, $_;} when (/;CC/) {push @CCC, $_;} when(!/;AA/ && !/;BB/ && !/;CC/) {push @DDD, $_;} default {} } unshift @AAA, $Header; unshift @BBB, $Header; unshift @CCC, $Header; unshift @DDD, $Header; print AAA @AAA; print BBB @BBB; print CCC @CCC; print DDD @DDD; close (AAA); close (BBB); close (CCC); close (DDD);
But I tried to shorten the script with some loops. The following script brings error:
use 5.010; use strict; use warnings; use FileHandle; my $Year = "2011"; my $Month = "04"; my $address = "D:/myfolder/".$Year."/".$Month."/data/"; open(SOURCE, $address."TTT.txt") || die "$!"; my $fh; my @namen2 = qw/ AAA BBB CCC DDD /; my %fh = map {$_, $fh = FileHandle->new(">D:/myfolder/".$Year."/".$Mon +th."/".$_."/".$_.".txt");} @namen2; my @AAA; my @BBB; my @CCC; my @DDD; my @contents=<SOURCE>; close (SOURCE); my $Header = shift @contents; my %testhash = ("AAA" => \@AAA, "BBB" => \@BBB, "CCC" => \@CCC, "DDD" => \@DDD, ); foreach(@contents) { when (/;AA/) {push @AAA, $_;} when (/;BB/) {push @BBB, $_;} when (/;CC/) {push @CCC, $_;} when(!/;AA/ && !/;BB/ && !/;CC/) {push @DDD, $_;} default {} } foreach (keys %testhash) { unshift @{ $testhash{$_} }, $Header; } foreach (keys %fh) { my $x = $_; foreach (keys %testhash) { my $y = $_; if ($x eq $y) {print $fh{$x} @{ $testhash{$y}};} } } foreach (keys %fh) {close $fh{$_};}
The error message by trying hash comparison (%fh and %testhash): "Array found where operator expected" and then "syntax error". I tried this with smart match, no success either. Please help! Many thanks in advance!

In reply to Won Doc by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.