reading file and storing information of lines with varying length

Discussion in 'Perl Misc' started by christrier, Nov 9, 2005.

  1. christrier

    christrier Guest

    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
     
    christrier, Nov 9, 2005
    #1
    1. Advertising

  2. 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.
     
    Andrew McGregor, Nov 9, 2005
    #2
    1. Advertising

  3. christrier

    christrier Guest

    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!
     
    christrier, Nov 9, 2005
    #3
  4. christrier

    Anno Siegel Guest

    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.
     
    Anno Siegel, Nov 9, 2005
    #4
  5. christrier

    Guest

    "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
     
    , Nov 9, 2005
    #5
  6. christrier

    christrier Guest

    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.
     
    christrier, Nov 15, 2005
    #6
  7. christrier

    christrier Guest


    > 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
     
    christrier, Nov 15, 2005
    #7
  8. 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
     
    Tad McClellan, Nov 15, 2005
    #8
  9. 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::Dumper module:


    use Data::Dumper;

    # ... code that loads %some_hash

    print Dumper \%some_hash;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 15, 2005
    #9
  10. christrier

    Guest

    "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
     
    , Nov 15, 2005
    #10
  11. On 2005-11-15, Tad McClellan <> wrote:
    > 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().


    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

    --
    David H. Adler - <> - http://www.panix.com/~dha/
    "Last year in Oregon, Summer fell on a *tuesday*. That was it. One
    day. Big shiny thing in the sky. Some people thought it was a UFO."
    - Randal Schwartz in comp.lang.perl.misc
     
    David H. Adler, Nov 15, 2005
    #11
  12. christrier

    christrier Guest

    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
     
    christrier, Nov 15, 2005
    #12
  13. christrier

    Bart Lateur Guest

    christrier wrote:

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


    It's functionality coded in the module Data::Dumper. It comes with perl,
    standard.

    <http://perldoc.perl.org/Data/Dumper.html>

    --
    Bart.
     
    Bart Lateur, Nov 16, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Wright
    Replies:
    0
    Views:
    544
    Joe Wright
    Jul 27, 2003
  2. Replies:
    0
    Views:
    268
  3. Luiz Antonio Gomes Pican?o

    Storing variable length data in file -- Paging

    Luiz Antonio Gomes Pican?o, Feb 25, 2005, in forum: C Programming
    Replies:
    15
    Views:
    569
    Dave Thompson
    Mar 7, 2005
  4. Java and Swing

    Split string whose length is varying

    Java and Swing, Oct 6, 2005, in forum: C Programming
    Replies:
    9
    Views:
    336
    Christopher Benson-Manica
    Oct 6, 2005
  5. Replies:
    1
    Views:
    476
    Jonathan Mcdougall
    Dec 6, 2005
Loading...

Share This Page