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

Re: [PBML] Working with Lists

Expand Messages
  • Dan Boger
    ... try this: while ( ) { @fields = split /,/, $_; $hash{$fields[1]} = [] unless exists $hash{$fields[1]}; push @{$hash{$fields[1]}}, $fields[0]; } foreach
    Message 1 of 6 , Aug 7, 2000
      On Mon, Aug 07, 2000 at 03:47:24PM +0100, John Davidson wrote:
      > I hope someone can help
      >
      > I have a list like this:
      >
      > DEL09A2,053.0009.34,TAD2K09,
      > DEL09A2,074.1001.30,LOST001,
      > DEL09A2,845.9112.22,RUG112CD,
      > DEL09A3,845.9112.22,RUG112CD,
      >
      > What I need to do is format it like this
      >
      > 053.0009.34,DEL09A2
      > 074.1001.30,DEL092A2
      > 845.9112.22,DEL09A2,DEL09A3
      >
      > i.e for each unique occurrence of field 2, print each occurrence of field 1
      >
      > I've tried to work it out, but I'm completely lost. Can anyone suggest a
      > way around this?

      try this:

      while (<>) {
      @fields = split /,/, $_;
      $hash{$fields[1]} = [] unless exists $hash{$fields[1]};
      push @{$hash{$fields[1]}}, $fields[0];
      }

      foreach (sort keys %hash) {
      print "$_ -> @{$hash{$_}}\n";
      }


      Hope that helps,

      Dan
    • David Rigaudiere
      Hi! try this : while ( ) { ($field1, $field2) = split(/,/, $_); print $field2, $field1 n ; } David Sniper Rigaudiere ... From: John Davidson
      Message 2 of 6 , Aug 7, 2000
        Hi!
        try this :


        while (<>) {
        ($field1, $field2) = split(/,/, $_);
        print "$field2, $field1\n";
        }

        David "Sniper" Rigaudiere

        ----- Original Message -----
        From: "John Davidson" <john.davidson@...>
        To: "Perl-Beginner (E-mail)" <perl-beginner@egroups.com>
        Sent: Monday, August 07, 2000 4:47 PM
        Subject: [PBML] Working with Lists


        I hope someone can help

        I have a list like this:

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

        What I need to do is format it like this

        053.0009.34,DEL09A2
        074.1001.30,DEL092A2
        845.9112.22,DEL09A2,DEL09A3

        i.e for each unique occurrence of field 2, print each occurrence of field 1

        I've tried to work it out, but I'm completely lost. Can anyone suggest a
        way around this?


        Thanks


        John Davidson
      • David Rigaudiere
        ARF!!! ... i m so sorry David
        Message 3 of 6 , Aug 7, 2000
          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 4 of 6 , Aug 7, 2000
            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 5 of 6 , Aug 7, 2000
              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.