Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
use strict; use warnings; use Data::Dumper; my %hash; my @names; while(<DATA>) { my ($Groupname,@Data) = split(/\s+/,$_); foreach(@Data) { my ($name,$count) = split(/\|/,$_); unless ( grep( /^$name$/, @names ) ) { push(@names,$name); } $hash{$Groupname}->{$name}++; } } &Format(\%hash,\@names); sub Format { my ($hash_ref,$name_ref) = @_; my @Names = sort @$name_ref; my @Group_names = keys %{$hash_ref}; my $filename = "test.txt"; #Give File Path open my $fh, '>>', $filename or die "Can't write to '$filename +': $!\n"; print $fh "\t@Names\n"; foreach my $g_name (@Group_names) { print $fh "$g_name\t"; foreach my $name(@Names) { unless (exists $hash_ref->{$g_name}->{$name}) { print $fh qq{0\t}; } else { print $fh $hash_ref->{$g_name}->{$name}.qq{\t}; } } print OUT qq{\n}; } } __DATA__ Group1: somo|112345478 somo|734567233 homo|233689876 Group2: somo|904686712 somo|891145662 somo|106736432 Group3: aomo|397634567 aomo|123446789 Group4: aomo|905672345 aomo|120846789

Use open() function to open the input file and read each line using while loop, instead of using <DATA>


Updated the file open() safer method, as per Arunbear advise! Thanks Arunbear ++

In reply to Re: Need help in file processing by vinoth.ree
in thread Need help in file processing by utpalmtbi

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [virtualsue]: 2 people - woot
    [virtualsue]: an exclusive club
    marto waves
    choroba sees 5
    Discipulus more.. and count cloaked monks..
    shmem joins the club
    [Discipulus]: welcome back to Tkperlmonks virtualsue! ;=)
    [virtualsue]: I missed my 17 year anniversary

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2018-04-24 11:00 GMT
    Find Nodes?
      Voting Booth?