Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > reading file and storing information of lines with varying length

Reply
Thread Tools

reading file and storing information of lines with varying length

 
 
christrier
Guest
Posts: n/a
 
      11-09-2005
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

 
Reply With Quote
 
 
 
 
Andrew McGregor
Guest
Posts: n/a
 
      11-09-2005
christrier wrote:
> 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.
 
Reply With Quote
 
 
 
 
christrier
Guest
Posts: n/a
 
      11-09-2005
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!

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-09-2005
christrier <> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      11-09-2005
"christrier" <> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
christrier
Guest
Posts: n/a
 
      11-15-2005
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.

 
Reply With Quote
 
christrier
Guest
Posts: n/a
 
      11-15-2005

> 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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-15-2005
christrier <> wrote:

> I don't really get what
> you mean!
>> my (undef,undef,undef,undef,undef,@pairs)=split/;/, $line;


> 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().


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-15-2005
christrier <> wrote:

>> push @{$some_hash{$k}},$v;



> 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:umper module:


use Data:umper;

# ... code that loads %some_hash

print Dumper \%some_hash;


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      11-15-2005
"christrier" <> wrote:
> 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!

> > 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!


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.

>
> > 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


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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing lines from a text file bradfordh@gmail.com Python 0 01-29-2006 11:00 PM
How to display a string in many lines, each lines have a specified length thuyptt@dsp.com.vn C++ 1 12-06-2005 07:26 AM
Split string whose length is varying Java and Swing C Programming 9 10-06-2005 07:19 PM
Storing variable length data in file -- Paging Luiz Antonio Gomes Pican?o C Programming 15 03-07-2005 08:44 AM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57