Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: construct a matrix by different intervals

by fmerges (Chaplain)
on Aug 19, 2007 at 19:34 UTC ( [id://633647]=note: print w/replies, xml ) Need Help??


in reply to construct a matrix by different intervals

Hi,

You can use a HoH

my $result = { 'A' => { 1 => 1, 2 => 1, 7 => 1, 10 => 1, ... }, 'B' => { ... }, ... };

Using something like this for generating it:

open my $fh, '<', $filename; while (<$fh>) { chomp; my ($id, @numbers) = split; for my $number (@numbers) { $result->{$id}{$number} = 1; } }

Update:

Later if you want to output it, you could do something like this:

my %unique_numbers = (); for (values %$result) { for my $number (keys %$_) { $unique_numbers{$number} = 1; } } my @numbers = sort {$a <=> $b} keys %unique_numbers; open my $out_fh, '>', $output_filename; print $out_fh "IDs @numbers\n"; for my $id (sort keys %$result) { my @fields = $id; for my $number (@numbers) { if (exists $result->{$id}{$number}) { push @fields, 1; } else { push @fields, 0; } } my $string = join " ", @fields; print $out_fh "$string\n"; }

I know, it's not so nice, but... if it's a throw away problem...

For the thing of having a fancy output, you can use format or take a look on CPAN, I remember to see some modules for producing a tabular ASCII output

Update 2:

Yes GrandFather, you're right, I just saw the "interval" thing :-)

Regards,

fmerges at irc.freenode.net

Replies are listed 'Best First'.
Re^2: construct a matrix by different intervals
by GrandFather (Saint) on Aug 19, 2007 at 22:18 UTC

    Consider what is generated for the OP's data line 'D 10 20' and contrast with OP's desired result. The task is not quite so simple. ;)


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://633647]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2025-11-13 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (69 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.