Generating a Hash Problem

  • Thread starter Stephanie Kroeplin
  • Start date
S

Stephanie Kroeplin

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
 
G

Gunnar Hjalmarsson

Stephanie said:
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.
 
A

Anno Siegel

Stephanie Kroeplin said:
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
 
I

ioneabu

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
 
I

ioneabu

hey,

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

wana
 

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