Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merging files with heading 2

Status
Not open for further replies.

billbose

Programmer
Jan 15, 2001
66
US

I have a collection of text documents(ascii file name and the heading:

my %doc_collection = (


'joe.txt' => "Joes Collection"
'john.txt' => "Johnson's Collection"
'mary.txt' => "Mary's notes"
'liz.txt' => "Liz's articles"
.........

) ;

In order to open 'ONE' ascii document, say joe's text and put that in a array named user, I would have to,

my $file = '/joe.txt';
open(FILE, "$file");
my @users = <FILE>;
close FILE;

However I have to fill up the same @users array like this with docs for multiple users instead of just one.

@users = { "Joes collection", joe.txt content goes here, "Johnsons Collection", john.txt content goes here, "marys notes" , Marys notes come here, ........}


Can somebody tell the algorithm for this?
TIA
 
I would use a hash:
%hash{
'Joe's Collection' => 'Joe's text content'
'Johnson's Collection => 'Johnson's text'
}

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
I agree with Tom that it would be better to make %users a hash. Here's some code:
Code:
#!perl
use strict;
use warnings;
use Data::Dumper;

my %doc_collection = (
    'joe.txt' => "Joes Collection",
    'john.txt' => "Johnson's Collection",
    'mary.txt' => "Mary's notes",
    'liz.txt'  => "Liz's articles",
    );

my %users;
{
#    local $/; # enable slurp mode
    for my $filename (keys %doc_collection) {
        unless (open(FH, $filename)) {
            warn qq(Can't open file "$filename". Skipping...\n);
            next;
        }
        @{$users{$doc_collection{$filename}}} = <FH>; 
        close(FH) || die qq(Can't close file "$filename". Must quit!\n)
    }
}

my $d = Data::Dumper->new([\%users], [qw(*users)]);
print $d->Dump;

__END__

Output:
%users = (
           'Mary\'s notes' => [
                                'Mary had a little text.
',
                                'Whitespace throughout y\'know.
',
                                'It got Mary quite perplexed
',
                                'How it kept on to grow.'
                              ],
           'Johnson\'s Collection' => [
                                        'Here\'s John\'s text.
',
                                        'Here\'s the second line.
',
                                        'And here is the third.'
                                      ],
           'Joes Collection' => [
                                  'This is joe\'s text.
',
                                  'Blah, blah, blah.
'
                                ],
           'Liz\'s articles' => [
                                  'You make me dizzy Miss Lizzy
',
                                  'The way you rock and roll.'
                                ]
         );
I've made %users a hash of arrays. If you'd rather have the file contents stored as strings:

1. uncomment # local $/; # enable slurp mode
2. change @{$users{$doc_collection{$filename}}} to
$users{$doc_collection{$filename}}
(take off the leading @ and the outermost set of {}.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top