reading file and storing information of lines with varying length

C

christrier

Hi!

I have a problem with a file. It looks like this
;r;information;more_info;1;key1;value1;
;s;more;more;2;key1;value1;key2;value2;
;t;info;and;4;key1;value1;key2;value1;key1;value3;key2;value4

I have to extract the information in key and value of each line and
compare it to some information in another file.
I would like to read the file and store the key;value pairs into a hash
and then compare it later on with the other information I have.
As you can see (hopefully) in my example (i.e. line 3) I have some keys
that are used twice (or more often). My suggestion would be an hash of
arrays.
I think in the end it should "look" like this: (for line 3)
%name_of_hash = (
key1 => ["value1"], ["value3"],
key2 => ["value1"], ["value4"]
);
The file uses ";" as a delimiter and in field 4 you have information
about how many key;value-pairs there will be.

I hope I could make myself clear about the problem. I want to read a
file line by line and compare it with some other file. But at the
moment I don't know how to store the information in a hash!
If someone had an idea that would be great!

Greetings
Chris
 
A

Andrew McGregor

christrier said:
I don't know how to store the information in a hash!

Chapter 5. of the Perl Cookbook has some recipes on Hashes and storing
multiple values per key.

Otherwise a google for `Perl "hash of hash"' will turn up plenty of
examples and tutorials.
 
C

christrier

I guess you missunderstood me!
I know how to store information in a hash but at the moment I don't
know how to get the information that is written in the line!
 
A

Anno Siegel

christrier said:
I guess you missunderstood me!
I know how to store information in a hash but at the moment I don't
know how to get the information that is written in the line!

So what have you tried, and how does it fail? For a start, see
perldoc -f split.

Anno
 
X

xhoster

christrier said:
Hi!

I have a problem with a file. It looks like this
;r;information;more_info;1;key1;value1;
;s;more;more;2;key1;value1;key2;value2;
;t;info;and;4;key1;value1;key2;value1;key1;value3;key2;value4

I have to extract the information in key and value of each line and
compare it to some information in another file.
I would like to read the file and store the key;value pairs into a hash
and then compare it later on with the other information I have.
As you can see (hopefully) in my example (i.e. line 3) I have some keys
that are used twice (or more often). My suggestion would be an hash of
arrays.
I think in the end it should "look" like this: (for line 3)
%name_of_hash = (
key1 => ["value1"], ["value3"],
key2 => ["value1"], ["value4"]
);
The file uses ";" as a delimiter and in field 4 you have information
about how many key;value-pairs there will be.

How about:

my (undef,undef,undef,undef,undef,@pairs)=split/;/, $line;
die "Odd number!" if @pairs%2;
while (my ($k,$v)=splice @pairs,0,2) {
push @{$some_hash{$k}},$v;
};

Xho
 
C

christrier

I am sorry, but I am quite a Newbie to Perl and I don't really get what
you mean!
my (undef,undef,undef,undef,undef,@pairs)=split/;/, $line;
what exactly does this line do? I think it reads the first five
statements into the undef-variable and then it reads the rest into the
@pairs-array - if i am wrong please correct me!
Why do you use 5 undef-variables?
die "Odd number!" if @pairs%2;
while (my ($k,$v)=splice @pairs,0,2) {
push @{$some_hash{$k}},$v;
};
this loop reads from position 0 to position 2 and writes everything
into the hash and "deletes entry 0 and 1 - so you can repeat the same
operation until the array is empty?
Again, if i am wrong please correct me.
 
C

christrier

my (undef,undef,undef,undef,undef,@pairs)=split/;/, $line;
die "Odd number!" if @pairs%2;
while (my ($k,$v)=splice @pairs,0,2) {
push @{$some_hash{$k}},$v;
};

I already said that I am a Newbie.
I have another question: is this an array of hashes what you used or
and hash of arrays? I am not quite sure which one you used.

Best regards
Chris
 
T

Tad McClellan

christrier said:
I don't really get what
you mean!
what exactly does this line do?


It discards the 1st 5 elements of the list returned from the split().

This use of undef is documented in the "List value constructors" section in:

perldoc perldata

An exception to this is that you may assign to C<undef> in a list.
This is useful for throwing away some of the return values of a
function

Why do you use 5 undef-variables?


To discard the 1st 5 elements of the list returned from the split().
 
T

Tad McClellan

christrier said:
I already said that I am a Newbie.


You don't need to repeat it in each post.

I have another question: is this an array of hashes what you used or
and hash of arrays? I am not quite sure which one you used.


You can answer this question yourself by using the Data::Dumper module:


use Data::Dumper;

# ... code that loads %some_hash

print Dumper \%some_hash;
 
X

xhoster

christrier said:
I am sorry, but I am quite a Newbie to Perl and I don't really get what
you mean!

I only included code. I didn't "mean" anything by it, it was just code!
what exactly does this line do? I think it reads the first five
statements into the undef-variable and then it reads the rest into the
@pairs-array - if i am wrong please correct me!

Well, undef isn't a variable, it is just a place holder. And split doesn't
return statements, it returns strings. Statements are code, strings are
data.
Why do you use 5 undef-variables?

Because your data lines start with 5 fields that are not interesting.
this loop reads from position 0 to position 2

It is a length of 2 starting from 0, so that means position 0 to
position *1*. And I wouldn't call it "reading", but rather "assigning".
and writes everything
into the hash and "deletes entry 0 and 1 - so you can repeat the same
operation until the array is empty?

Pretty much, yes.

Xho
 
D

David H. Adler

It discards the 1st 5 elements of the list returned from the split().

In the spirit of TMTOWTDI...

my @pairs = split /;/, $line;
splice @pairs, 0, 5;

Is almost as fast (unless you're doing this A REALLY BIG WHOLE LOT) and
possibly clearer.

dha
 
C

christrier

Thank you all for your help!

I am now able to extract the information I need from a line and compare
it with values from a line in an other file!

Well, I guess I'll have to learn much more about perl because I didn't
know what the Dumper-function does.

Greetings,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top