Generating a Hash Problem

Discussion in 'Perl Misc' started by Stephanie Kroeplin, Feb 2, 2005.

  1. All,

    I have an array of strings lets say @records. The strings are records that
    are comma-separated (i am shortening them to save typing though). IE:
    $records[0] = '77856, LP3-13,stephk,1234'
    $records[1] = '77857,LP3-13,stephk,1235'
    ....and so on

    I also have an array of strings (@field_names) =
    [record_id,printer_name,user_name,print_count]. These correspond to each
    comma-seperated string in @records.

    I would like to step thru each element in @records, using split turn each
    comma-separated string into an array, an generate a hash that is like this:

    %HASH {
    record0 =>{ record_id=>77856
    printer_name=>'LP3-13'
    user_name=>'stephk'
    print_count=>1234
    }
    record1 =>{ record_id=>77857
    printer_name=>'LP3-13'
    user_name=>'stephk'
    print_count=>1235
    }
    }

    What is the best way to get this done? I have a lot of records.

    Any help would be awesome! Thanks,

    stephanie
    Stephanie Kroeplin, Feb 2, 2005
    #1
    1. Advertising

  2. Stephanie Kroeplin wrote:
    > I have an array of strings lets say @records. The strings are records that
    > are comma-separated (i am shortening them to save typing though). IE:
    > $records[0] = '77856, LP3-13,stephk,1234'
    > $records[1] = '77857,LP3-13,stephk,1235'
    > ...and so on
    >
    > I also have an array of strings (@field_names) =
    > [record_id,printer_name,user_name,print_count]. These correspond to each
    > comma-seperated string in @records.
    >
    > I would like to step thru each element in @records, using split turn each
    > comma-separated string into an array, an generate a hash that is like this:
    >
    > %HASH {
    > record0 =>{ record_id=>77856
    > printer_name=>'LP3-13'
    > user_name=>'stephk'
    > print_count=>1234
    > }
    > record1 =>{ record_id=>77857
    > printer_name=>'LP3-13'
    > user_name=>'stephk'
    > print_count=>1235
    > }
    > }
    >
    > What is the best way to get this done?


    Let us know which ways you are choosing between, and I'm sure people
    will let you know which one they prefer.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 2, 2005
    #2
    1. Advertising

  3. Stephanie Kroeplin

    Anno Siegel Guest

    Stephanie Kroeplin <> wrote in comp.lang.perl.misc:
    > All,
    >
    > I have an array of strings lets say @records. The strings are records that
    > are comma-separated (i am shortening them to save typing though). IE:
    > $records[0] = '77856, LP3-13,stephk,1234'
    > $records[1] = '77857,LP3-13,stephk,1235'
    > ...and so on
    >
    > I also have an array of strings (@field_names) =
    > [record_id,printer_name,user_name,print_count]. These correspond to each
    > comma-seperated string in @records.
    >
    > I would like to step thru each element in @records, using split turn each
    > comma-separated string into an array, an generate a hash that is like this:
    >
    > %HASH {
    > record0 =>{ record_id=>77856
    > printer_name=>'LP3-13'
    > user_name=>'stephk'
    > print_count=>1234
    > }
    > record1 =>{ record_id=>77857
    > printer_name=>'LP3-13'
    > user_name=>'stephk'
    > print_count=>1235
    > }
    > }
    >
    > What is the best way to get this done? I have a lot of records.


    The best? Would you settle for one way?

    You could use a sub that converts one record to a hashref:

    sub convert_record {
    my %h;
    @h{ @field_names} = split /,\s*/, shift;
    \ %h;
    }

    To convert the whole list in place:

    $_ = convert_record( $_) for @records;

    That gives you a list of hashes, not a hash of hashes. You may want
    to leave it at that, but if you actually need the hash structure with
    "artificial" keys, you can build it from there:

    my %hash = map { "record$_" => $records[ $_] } 0 .. $#records;

    (All untested)

    Anno
    Anno Siegel, Feb 2, 2005
    #3
  4. Stephanie Kroeplin

    Guest

    tested:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my @records = (<>);
    chomp @records;
    for (@records)
    {

    my %hash;
    @hash{('record_id', 'printer_name', 'user_name',
    'print_count')} =
    split ',';
    $_ = \%hash;
    }

    use Data::Dumper;

    print Dumper $_ for @records;
    # put your data in a file.txt and do: cat file.txt | ./this.pl
    , Feb 2, 2005
    #4
  5. Stephanie Kroeplin

    Guest

    hey,

    I just realized I did it the same way you did but your code is fancier
    :)

    wana
    , Feb 2, 2005
    #5
    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. tdopsdarock

    Errors in generating hash

    tdopsdarock, Mar 3, 2008, in forum: C Programming
    Replies:
    0
    Views:
    317
    tdopsdarock
    Mar 3, 2008
  2. smith jack
    Replies:
    0
    Views:
    149
    smith jack
    Aug 2, 2011
  3. Bshowers
    Replies:
    0
    Views:
    182
    Bshowers
    Nov 12, 2004
  4. rp
    Replies:
    1
    Views:
    512
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    608
    David A. Black
    Jul 2, 2008
Loading...

Share This Page