Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Working with Lists

Expand Messages
  • David Rigaudiere
    ARF!!! ... i m so sorry David
    Message 1 of 6 , Aug 7, 2000
    • 0 Attachment
      ARF!!!
      >> for each unique occurrence of field 2

      i'm so sorry

      David
    • Andrew Johnson
      Whenever you find yourself thinking about keeping track of unique items, you should think of using a hash. If you are familiar with using references, here is
      Message 2 of 6 , Aug 7, 2000
      • 0 Attachment
        Whenever you find yourself thinking about keeping track of unique
        items, you should think of using a hash. If you are familiar with
        using references, here is one solution:


        #!/usr/bin/perl -w
        use strict;

        my %data;

        while(<DATA>){
        chomp;
        my @fields = split /,/;
        push @{$data{$fields[1]}}, $fields[0];
        }

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

        __DATA__
        DEL09A2,053.0009.34,TAD2K09,
        DEL09A2,074.1001.30,LOST001,
        DEL09A2,845.9112.22,RUG112CD,
        DEL09A3,845.9112.22,RUG112CD,

        The above makes use of references -- in particular, by dereferencing
        $data{$fields[1]} as if it were an array reference, perl
        autovivifies an anonymous array reference for us to use.

        You could also do it without using references -- rather than pushing
        all of the field 1 values onto an array for each unique field 2
        value, we could just keep the value as a string and just concatenate
        each new value onto it:

        #!/usr/bin/perl -w
        use strict;

        my %data;

        while(<DATA>){
        chomp;
        my @fields = split /,/;
        $data{$fields[1]} .= ",$fields[0]";
        }

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

        __DATA__
        DEL09A2,053.0009.34,TAD2K09,
        DEL09A2,074.1001.30,LOST001,
        DEL09A2,845.9112.22,RUG112CD,
        DEL09A3,845.9112.22,RUG112CD,


        Both of the above methods must read in the entire file (creating the
        data structure as we go) before it can produce the proper output.
        However, if your data file is already sorted on the second field (so
        that any duplicates of field 2 occur in succession) you could produce
        output as you go without reading the whole file into memory at once
        -- but unless memory contraints prohibit it, I'd go with the first
        method above as it is more versatile if you want to change the output
        format (you only need change one character if you want the output
        separated by | characters instead of commas).

        Hope it helps,
        andrew

        --
        Andrew L. Johnson http://members.home.net/andrew-johnson/
        I drink to make other people interesting.
        -- George Jean Nathan
      • John Davidson
        Fantastic!! Thanks to all! For those who are interested, I played around with a un*x script to do the same: for i in `cat loc.txt | cut -d , -f2 | sort -u`
        Message 3 of 6 , Aug 7, 2000
        • 0 Attachment
          Fantastic!! Thanks to all!

          For those who are interested, I played around with a un*x script to do the
          same:

          for i in `cat loc.txt | cut -d',' -f2 | sort -u`
          do
          echo "$i,\c" >> op
          for j in `cat loc.txt | grep $i | cut -d',' -f1`
          do
          echo "$j,\c" >> op
          done
          echo >> op
          done

          I must put more effort into learning Perl :-)


          John
        Your message has been successfully submitted and would be delivered to recipients shortly.