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

Hello Monks,

I'm a newbie and I need some help with the following: This is my text file which I'm reading a string and if the string is shown later in my text file. I want to group them together and print it out according to the strings that match. I don't know if I'm wording this correctly but if you don't understand what I'm trying to say, I will try to rephrase it. Thanks!!

Kim,0,0,off
Kim,1,1,on
Tyra,0,0,off
Tyra,1,1,on
Rada,0,0,zero
Rada,1,1,one

My output is suppose to look like this:
Kim,on,off
Tyra,on,off
Rada,one, zero

Replies are listed 'Best First'.
Re: Matching Text
by EvdB (Deacon) on Jun 10, 2003 at 17:51 UTC
    I'm not quite sure what you are trying to do - which tends to mean that nor are you.

    You might want to rephrase the problem into pseudo code, something like this:

    #for each line { # split line into bits; # has string been seen { # do something; # } #}
    If you get this bit right then you can turn the pseudo code into real code fairly easily. It will also hep you keep track of what you are doing. I recall being a beginner and how easy it was getting lost in even a short bit of code. This technique I found very helpful.

    If you are still stuck then post back the pseudo-code and someone will probably help you fill it. However without the pseudo code it is difficult to know exactly what you want to achieve...

    --tidiness is the memory loss of environmental mnemonics

Re: Matching Text
by pzbagel (Chaplain) on Jun 10, 2003 at 17:42 UTC

    This sounds like homework. Are you sure you don't want to give this a whirl? It's a fairly basic script which reads a line, splits it, and stores the result in some sort of data structure. Why don't you try it yourself and post your code if you get stuck. The monks here could whip up a real slick perlish way to do it, but would your teacher really believe you wrote it if you are just starting out in perl? Plus, if someone else does all the work, you stay a newbie.

    "You learn by doing", as my chemistry teacher used to say.

Re: Matching Text
by sauoq (Abbot) on Jun 10, 2003 at 18:51 UTC
    $ perl -F, -lane 'push@{$h{$F[0]}},$F[3]}{$"=",";print"$_,@{[reverse@{ +$h{$_}}]}"for sort keys%h' < input.txt Kim,on,off Rada,one,zero Tyra,on,off

    I wouldn't turn that in if I were you though...

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Matching Text
by CountZero (Bishop) on Jun 10, 2003 at 18:01 UTC

    Another question: are the second and third fields of each record important or can they be thrown away?

    Assuming they are unimportant, I suggest you use a hash of arrays (if you want to keep all fourth fields, even if they are repeated) or a hash of hashes (if you want to collapse repeated fourth fields into one item).

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Matching Text
by Cody Pendant (Prior) on Jun 10, 2003 at 23:10 UTC
    Does your sample data really contain "zero" instead of "0" in some cases?

    I'm wondering if this is some kind of trick your teacher is playing on you to see if you spot that "zero" can't be numerically sorted or something?
    --

    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: Matching Text
by cciulla (Friar) on Jun 10, 2003 at 18:08 UTC

    This may be what you're looking for:

    #!/usr/bin/perl use strict; use warnings; my %names; while (<DATA>) { chomp; my ($name, $lastThingy) = (split /,/)[0,3]; push @{$names{$name}}, $lastThingy; } foreach my $key (keys %names) { my $values = join ',', (@{$names{$key}}); print "$key,$values\n"; } __DATA__ Kim,0,0,off Kim,1,1,on Tyra,0,0,off Tyra,1,1,on Rada,0,0,zero Rada,1,1,one

    Update: v5.6.1 built for cygwin-multi doesn't print out the key, but v5.8.0 built for MSWin32-x86-multi-thread does. Weird.