Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Generating a Hash Problem

Reply
Thread Tools

Generating a Hash Problem

 
 
Stephanie Kroeplin
Guest
Posts: n/a
 
      02-02-2005
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


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-02-2005
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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      02-02-2005
Stephanie Kroeplin <(E-Mail Removed)> 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
 
Reply With Quote
 
ioneabu@yahoo.com
Guest
Posts: n/a
 
      02-02-2005
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:umper;

print Dumper $_ for @records;
# put your data in a file.txt and do: cat file.txt | ./this.pl

 
Reply With Quote
 
ioneabu@yahoo.com
Guest
Posts: n/a
 
      02-02-2005
hey,

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


wana

 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
how to sort a hash list without generating a new object? smith jack Python 0 08-02-2011 06:02 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Errors in generating hash tdopsdarock C Programming 0 03-03-2008 01:24 AM
Problem generating MD5 hash from asp classic like .NET Bshowers ASP General 0 11-12-2004 12:22 AM



Advertisments