Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Simple Table structure to file

by ultibuzz (Monk)
on Jan 23, 2007 at 09:15 UTC ( #596052=perlquestion: print w/replies, xml ) Need Help??

ultibuzz has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
i have the following data

12;22533721 12;18217115 12;23517705 12;28196422 12;26247514 12;26235466 12;26461322 12;23653293 12;18850798 12;23694918 12;28544046 12;26668890 12;26501989 12;26192004 12;23331033 12;19781377 12;25114470 12;28433017 12;27228379 12;27381735 12;27235837 12;25695918 12;25776071 12;21933659 12;19778625 12;18136550 12;23172711 12;23192154 12;21052533 12;20162237 12;19371167 01;33723276 01;27077592 01;25222950 01;24774003 01;22267932 01;19688033 01;23363461 01;27152096 01;25984687 01;24855444 01;26059561 01;22790798 01;18620768 01;22862582 01;25991422 01;25314897 01;24899719 01;31966695 01;25828569 01;18532027 01;22411384

and i want somthing like this
01;18532027;18532027;... 12;28532027;18532027...

so that all values with 01 stand behind each other.

i want to use a HoA because i've done it lots of times before but at the moment im stuck with this.
any tips and hints will be usefull because im now thinking of this for about 1 hour

kd ultibuzz

Replies are listed 'Best First'.
Re: Simple Table structure to file
by davorg (Chancellor) on Jan 23, 2007 at 09:23 UTC

    A hash of arrays sounds like the right approach to me. What code do you have so far?

    As for hints and tips, try reading the appropriate section of perldsc.


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Simple Table structure to file
by virtualsue (Vicar) on Jan 23, 2007 at 09:41 UTC
    Since we all appear to be giving you different pieces of a solution:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %data; while (my $line = <DATA>) { chomp $line; [parse the input] [assign to your hash] } print Dumper \%data; # check to see if your code set up the HoA corre +ctly __DATA__ 01;22411384 01;18337723 02;38381289 02;38938222 10;13293383
Re: Simple Table structure to file
by wfsp (Abbot) on Jan 23, 2007 at 09:24 UTC
    Hi ultibuzz!

    It would be nice to see what you tried but here's my take on it.

    I reckon you would need two passes. The first to build the table and the second to sort it.

    The first pass could be to use a while loop over the data, chomp each line, split on ; and populate your table. The important line might look something like

    push @{$table{$key}}, $value;
    See if that helps and get back to us if you're still stuck.

    Update: Fixed typo. Thanks to davorg for spotting it.

Re: Simple Table structure to file
by shigetsu (Hermit) on Jan 23, 2007 at 09:20 UTC
    May we see what code you've written so far?
Re: Simple Table structure to file
by ultibuzz (Monk) on Jan 23, 2007 at 10:22 UTC

    after smathing and throwing some office stuff the blockade is gone and i managed to code somthing that is working but is far away from being nice code

    #!"C:\perl\bin\perl.exe" use warnings; use strict; open(IN, '<', "traffic_compare.txt") or die("open failed: $!"); my @tmp; while (<IN>) { chomp; push @tmp,(split(';',$_))[0]; } close(IN); my @tmp_new= do { my %seen;grep !$seen{$_}++, @tmp }; my %table_hoa; foreach my $groups (@tmp_new) { open(IN, '<', "traffic_compare.txt") or die("open failed: $!"); while (<IN>) { (my $id, my $value) = split(';',$_); if ($id == $groups) { push @{ $table_hoa{$groups} }, $value; } } close(IN); }

    thanks for the hints, and are there any idears how to amke this code a bit nicer ?
    i don't like this @tmp pushing everything in there removing the duplicates

    kd ultibuzz

      Actually, now I think about it, as you're potentially dealing with duplicates (I assume that's why you're using a %seen hash) then a hash of hashes might be simpler.

      my %data; while (<IN>) { chomp; my @vals = split /;/; $data{$vals[0]}{$vals[1]}++; }

      You then have a hash where the keys are your first set of values (12, 01, etc) and each value in the hash is reference to another hash where the keys are the second (longer) values and the values are the number of times each of those values appears in the data (which, I think, can be ignored for your purposes).

      Then you can get the output you want like this:

      foreach my $key (keys %data) { print "$key;"; print join(';', keys %{$data{$key}}); print "\n"; }

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      It seems to me that you are not taking advantage of the way hashes operate in Perl, so your code is doing far more work than necessary. If you use the code template I gave you, and plug in the split and the push, you'll be home in time for dinner. Try it with some sample data from your file in the __DATA__ section, or bang in your own file open and change the filehandle.
Re: Simple Table structure to file
by ultibuzz (Monk) on Jan 23, 2007 at 11:34 UTC

    thanks virtualsue and davorg,
    i forgot that a hash can only have unique keys.
    kd ultibuzz

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://596052]
Approved by wfsp
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2021-05-17 14:16 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (157 votes). Check out past polls.