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

Re: [PBML] sort a file by a field

Expand Messages
  • Charles K. Clarkson
    From: Denny Malloy ... security ... Assuming you want to write the file to a different file: my $in_file = in.txt ; my
    Message 1 of 5 , Mar 1, 2001
    • 0 Attachment
      From: "Denny Malloy" <denny.malloy@...>
      : Hi,
      :
      : Is there an easy way to sort a file by a field in that file using PERL?
      : I have an ASCII comma delimited file with name, address and social
      security
      : number. I need to sort this file by social security number only and write
      : it back out.
      :
      : Example of file:
      : name,address,city,state,zip,ssn

      Assuming you want to write the file to a different file:

      my $in_file = 'in.txt';
      my $sorted_file = 'out.txt';
      open (RAW, $in_file) || die "Can't open $in_file: $!";
      my %raw_file = map {reverse /^(.*)(\d{9})$/} <RAW>;
      close RAW;
      open (SORTED, ">$sorted_file") || die "Can't open $sorted_file: $!";
      print SORTED "$raw_file{$_}$_\n" for sort keys %raw_file;
      close SORTED;

      HTH,
      Charles K. Clarkson
    • Denny Malloy
      Hi What you sent works but there is still one problem. My file can have more than one of the same social security number. When you use the hash it only keeps
      Message 2 of 5 , Mar 1, 2001
      • 0 Attachment
        Hi

        What you sent works but there is still one problem. My file can have more
        than one of the same social security number. When you use the hash it only
        keeps the first line if you have multiple social security numbers. I need
        to keep all the lines.

        Thanks,

        Denny Malloy


        -----Original Message-----
        From: Charles K. Clarkson [mailto:c_clarkson@...]
        Sent: Thursday, March 01, 2001 2:27 PM
        To: perl-beginner@yahoogroups.com
        Subject: Re: [PBML] sort a file by a field


        From: "Denny Malloy" <denny.malloy@...>
        : Hi,
        :
        : Is there an easy way to sort a file by a field in that file using PERL?
        : I have an ASCII comma delimited file with name, address and social
        security
        : number. I need to sort this file by social security number only and write
        : it back out.
        :
        : Example of file:
        : name,address,city,state,zip,ssn

        Assuming you want to write the file to a different file:

        my $in_file = 'in.txt';
        my $sorted_file = 'out.txt';
        open (RAW, $in_file) || die "Can't open $in_file: $!";
        my %raw_file = map {reverse /^(.*)(\d{9})$/} <RAW>;
        close RAW;
        open (SORTED, ">$sorted_file") || die "Can't open $sorted_file: $!";
        print SORTED "$raw_file{$_}$_\n" for sort keys %raw_file;
        close SORTED;

        HTH,
        Charles K. Clarkson






        Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
      • Damien Carbery
        You can achieve this with my script by changing: $UnSorted{ $SSN } = $_; # Store whole line. to: # If SSN already found, append line to the hash value. if (
        Message 3 of 5 , Mar 1, 2001
        • 0 Attachment
          You can achieve this with my script by changing:
          $UnSorted{ $SSN } = $_; # Store whole line.
          to:
          # If SSN already found, append line to the hash value.
          if ( defined $UnSorted{ $SSN } )
          {
          $UnSorted{ $SSN } = $UnSorted{ $SSN } . "\n" . $_;
          }
          else
          {
          $UnSorted{ $SSN } = $_;
          }

          Charles - wow, what a short solution!

          > > --- In perl-beginner@y..., Denny Malloy <denny.malloy@n...> wrote:
          > > > Hi
          > > >
          > > > What you sent works but there is still one problem. My file can
          > > have more
          > > > than one of the same social security number. When you use the
          > hash
          > > it only
          > > > keeps the first line if you have multiple social security
          numbers.
          > > I need
          > > > to keep all the lines.
          > > >
          > > > Thanks,
          > > >
          > > > Denny Malloy
          > > >
          > > >
          > > > -----Original Message-----
          > > > From: Charles K. Clarkson [mailto:c_clarkson@h...]
          > > > Sent: Thursday, March 01, 2001 2:27 PM
          > > > To: perl-beginner@y...
          > > > Subject: Re: [PBML] sort a file by a field
          > > >
          > > >
          > > > From: "Denny Malloy" <denny.malloy@n...>
          > > > : Hi,
          > > > :
          > > > : Is there an easy way to sort a file by a field in that file
          > using
          > > PERL?
          > > > : I have an ASCII comma delimited file with name, address and
          > social
          > > > security
          > > > : number. I need to sort this file by social security number
          only
          > > and write
          > > > : it back out.
          > > > :
          > > > : Example of file:
          > > > : name,address,city,state,zip,ssn
          > > >
          > > > Assuming you want to write the file to a different file:
          > > >
          > > > my $in_file = 'in.txt';
          > > > my $sorted_file = 'out.txt';
          > > > open (RAW, $in_file) || die "Can't open $in_file: $!";
          > > > my %raw_file = map {reverse /^(.*)(\d{9})$/} <RAW>;
          > > > close RAW;
          > > > open (SORTED, ">$sorted_file") || die "Can't open $sorted_file:
          > $!";
          > > > print SORTED "$raw_file{$_}$_\n" for sort keys %raw_file;
          > > > close SORTED;
          > > >
          > > > HTH,
          > > > Charles K. Clarkson
          > > >
          > > >
          > > >
          > > >
          > > >
          > > >
          > > > Your use of Yahoo! Groups is subject to
          > > http://docs.yahoo.com/info/terms/
        • Denny Malloy
          Thanks Damien and Charles You both helped me out a lot!!!!!!!!!!!!!!!!!!!!! ... From: Damien Carbery [mailto:daymobrew@yahoo.com] Sent: Thursday, March 01,
          Message 4 of 5 , Mar 1, 2001
          • 0 Attachment
            Thanks Damien and Charles

            You both helped me out a lot!!!!!!!!!!!!!!!!!!!!!



            -----Original Message-----
            From: Damien Carbery [mailto:daymobrew@...]
            Sent: Thursday, March 01, 2001 3:28 PM
            To: perl-beginner@yahoogroups.com
            Subject: Re: [PBML] sort a file by a field


            You can achieve this with my script by changing:
            $UnSorted{ $SSN } = $_; # Store whole line.
            to:
            # If SSN already found, append line to the hash value.
            if ( defined $UnSorted{ $SSN } )
            {
            $UnSorted{ $SSN } = $UnSorted{ $SSN } . "\n" . $_;
            }
            else
            {
            $UnSorted{ $SSN } = $_;
            }

            Charles - wow, what a short solution!

            > > --- In perl-beginner@y..., Denny Malloy <denny.malloy@n...> wrote:
            > > > Hi
            > > >
            > > > What you sent works but there is still one problem. My file can
            > > have more
            > > > than one of the same social security number. When you use the
            > hash
            > > it only
            > > > keeps the first line if you have multiple social security
            numbers.
            > > I need
            > > > to keep all the lines.
            > > >
            > > > Thanks,
            > > >
            > > > Denny Malloy
            > > >
            > > >
            > > > -----Original Message-----
            > > > From: Charles K. Clarkson [mailto:c_clarkson@h...]
            > > > Sent: Thursday, March 01, 2001 2:27 PM
            > > > To: perl-beginner@y...
            > > > Subject: Re: [PBML] sort a file by a field
            > > >
            > > >
            > > > From: "Denny Malloy" <denny.malloy@n...>
            > > > : Hi,
            > > > :
            > > > : Is there an easy way to sort a file by a field in that file
            > using
            > > PERL?
            > > > : I have an ASCII comma delimited file with name, address and
            > social
            > > > security
            > > > : number. I need to sort this file by social security number
            only
            > > and write
            > > > : it back out.
            > > > :
            > > > : Example of file:
            > > > : name,address,city,state,zip,ssn
            > > >
            > > > Assuming you want to write the file to a different file:
            > > >
            > > > my $in_file = 'in.txt';
            > > > my $sorted_file = 'out.txt';
            > > > open (RAW, $in_file) || die "Can't open $in_file: $!";
            > > > my %raw_file = map {reverse /^(.*)(\d{9})$/} <RAW>;
            > > > close RAW;
            > > > open (SORTED, ">$sorted_file") || die "Can't open $sorted_file:
            > $!";
            > > > print SORTED "$raw_file{$_}$_\n" for sort keys %raw_file;
            > > > close SORTED;
            > > >
            > > > HTH,
            > > > Charles K. Clarkson
            > > >
            > > >
            > > >
            > > >
            > > >
            > > >
            > > > Your use of Yahoo! Groups is subject to
            > > http://docs.yahoo.com/info/terms/





            Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
          • Charles K. Clarkson
            From: Denny Malloy ... Oh sure! NOW you tell me: my %raw_file; my $in_file = in.txt ; my $sorted_file = out.txt ; open (RAW,
            Message 5 of 5 , Mar 1, 2001
            • 0 Attachment
              From: "Denny Malloy" <denny.malloy@...>
              : Hi
              :
              : What you sent works but there is still one problem. My file can have more
              : than one of the same social security number.

              Oh sure! NOW you tell me:

              my %raw_file;
              my $in_file = 'in.txt';
              my $sorted_file = 'out.txt';
              open (RAW, $in_file) || die "Can't open $in_file: $!";
              %raw_file = map { /(\d{9})$/ => $raw_file{$1} =
              $raw_file{$1} ? $raw_file{$1} . $_ :
              $_ } <RAW>;
              close RAW;
              open (SORTED, ">$sorted_file") || die "Can't open $sorted_file: $!";
              print SORTED "$raw_file{$_}" for sort keys %raw_file;
              close SORTED;

              HTH,
              Charles K. Clarkson


              : When you use the hash it only
              : keeps the first line if you have multiple social security numbers. I need
              : to keep all the lines.
              :
              : Thanks,
              :
              : Denny Malloy
              :
              :
              [SNIP]
            Your message has been successfully submitted and would be delivered to recipients shortly.