Separating array into alphabetical array of arrays 
Author Message
 Separating array into alphabetical array of arrays

Hi,

I would have thought you could do this by using the 'substr' command. What
you could do is, after sorting the array, step through each 'record' or
element taking the first letter and comparing it with the previous one. If
there it is different, then tell perl to create a new file.  To get the
first letter using substr try something like this...

(this code is not checked but it should give the idea)

# initialise the first letter to a, or whatever the first one might be
$previousLetter = 'a'


{
  # if the current letter is not equal to the previous one...
  if ( ! (substr($_, 0, 1) eq $previousLetter))
  { #... change the name of the output file which is being written to
    $outputFile  =  substr($_, 0, 1 ) . ".txt";
  }

  #write element to output file here  
 blah blah...

  # update the previous letter to be the current one for next iteration of loop.
  $previousLetter = substr($_, 0, 1);

Quote:
}

Hope this helps!....

T o b e s

Quote:

>I've recently completed a script that reads in a pipe deparate file of names,
>addresses, etc, sorts them, formats them and prints to file, thanks to help
>recieved from this group (thanks again!). Now here's what I need to do:

....
Quote:
>....Marc Bissonnette
>InternAlysis
>Corporate Internet Research and Results!
> http://www.*-*-*.com/



Sat, 24 Nov 2001 03:00:00 GMT  
 Separating array into alphabetical array of arrays
I've recently completed a script that reads in a pipe deparate file of names,
addresses, etc, sorts them, formats them and prints to file, thanks to help
recieved from this group (thanks again!). Now here's what I need to do:

Rather than print one long file sorted alphabetically, I need to have a
separate file for each letter (i.e. all names starting with 'a' then 'b', etc)

Can someone point me in the right direction to do this? (Either separating the
array into separate arrays by letter, or printing to a file depending on the
first letter of an array)

Many thanks,

--
----------------------------
Marc Bissonnette
InternAlysis
Corporate Internet Research and Results!
http://www.internalysis.com



Sun, 25 Nov 2001 03:00:00 GMT  
 Separating array into alphabetical array of arrays

hacked a version of perlbug to post the following to comp.lang.perl.misc:
: I've recently completed a script that reads in a pipe deparate file of names,
: addresses, etc, sorts them, formats them and prints to file, thanks to help
: recieved from this group (thanks again!). Now here's what I need to do:

Gratz!

: Rather than print one long file sorted alphabetically, I need to have a
: separate file for each letter (i.e. all names starting with 'a' then 'b', etc)

Ok.

: Can someone point me in the right direction to do this? (Either separating th
: array into separate arrays by letter, or printing to a file depending on the
: first letter of an array)

If it's sorted, just do something like ('like', in this case, meaning I
didn't test it):

my $last_letter = '';


    next unless /^([a-zA-Z])/;

    if ( lc $1 ne $last_letter ) {
        open OUT, "my_dir/$1" or
            die "Couldn't open my_dir/$1: $!\n";          
    }

    print OUT;

Quote:
}    

or, put it in a hash by letter:

my %letters;


    next unless /^([a-zA-Z])/;
    $letters{lc $1} = $_;

Quote:
}

: Many thanks,

HTH,

--Matthew



Sun, 25 Nov 2001 03:00:00 GMT  
 Separating array into alphabetical array of arrays


says...
...

Quote:
> If it's sorted, just do something like ('like', in this case, meaning I
> didn't test it):

I'll say you didn't!

Quote:
> my $last_letter = '';


>     next unless /^([a-zA-Z])/;

>     if ( lc $1 ne $last_letter ) {
>         open OUT, "my_dir/$1" or
>             die "Couldn't open my_dir/$1: $!\n";          
>     }

It might be nicer to open the file for output or appending.
And to set $last_letter so this doesn't happen on every line.

Quote:
>     print OUT;
> }    

...

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/



Sun, 25 Nov 2001 03:00:00 GMT  
 Separating array into alphabetical array of arrays

posted the following after running a magnifying glass over every message:
: It might be nicer to open the file for output or appending.
: And to set $last_letter so this doesn't happen on every line.

Awwww, that's no fun.

--Matthew



Mon, 26 Nov 2001 03:00:00 GMT  
 Separating array into alphabetical array of arrays

<snip variant already corrected by lr>

Quote:

> or, put it in a hash by letter:

> my %letters;


>     next unless /^([a-zA-Z])/;
>     $letters{lc $1} = $_;
> }

Unless you're planning on holding onto only the last line with each
beginning letter, I think you might want a hash of lists here:


     next unless /^([a-zA-Z])/;
     push ($letters{lc $1}, $_);

Quote:
}

And then of course, you loop through the keys of the hash and write each
list in your hash of lists out to a file whose name depends on the key.

It should also be noted that this method uses lots more memory, but
doesn't depend on the stuff being previously sorted.  The other method
depended on the data being sorted with (lc($a) cmp lc($b)).

--
Don Roby

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Mon, 26 Nov 2001 03:00:00 GMT  
 Separating array into alphabetical array of arrays


<snip>

Quote:


>      next unless /^([a-zA-Z])/;
>      push ($letters{lc $1}, $_);
> }


push does need an array, not a reference...

Sorry for talking to myself this way, but deja wouldn't let me cancel.
Guess it's time to get a real news setup.

Or maybe I should have just waited and let lr correct me... ;-)

--
Don Roby

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Mon, 26 Nov 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. URGENT: Hash in Hash, Array in Array, Array in Hash, Hash in Array

2. array of arrays of arrays

3. printing assoc array in non-alphabetical order

4. Help with Embedding and Arrays of Arrays?

5. Passing arrays of arrays in perlxs

6. arrays of arrays

7. Changing One Array Changes Unrelated Arrays -- Why?

8. (Q) array of associative arrays

9. Dividing an array into individual arrays.

10. Associative array of associate arrays

11. array of arrays?

12. Array of Associative Arrays?

 

 
Powered by phpBB® Forum Software