?
Solved

I need help creating a hash.  I am Stumped!

Posted on 2003-03-27
3
Medium Priority
?
146 Views
Last Modified: 2010-03-05
I need to create a hash by parsing the following information.  I would like my hash to look like this.

%Hashname (
            template:Actioncode => i
            SBID       => 100200401
            BusName    => Mikes System Test1
            City       => Douglas
            State      => MA
            Zip        => 01516
            Phone      => 5084742781
            CategoryID => 14438
);


This is the information that I would be parsing in the file.  The idea is to recreate the hash for each line below the template line.
I would greatly appreciate this.  One thing to note is that the script would have to be dynamic becuase the template line wont always look like that.  It could have extra fields.  Thank you!

template:Actioncode|SBID|BusName|City|State|Zip|Phone|CategoryID
i|100200401|Mikes System Test1|Douglas|MA|01516|5084742781|14438
i|100200402|Mikes System Test2|Douglas|MA|01516|5084742782|14438
i|100200403|Mikes System Test3|Douglas|MA|01516|5084742783|14438
i|100200404|Mikes System Test4|Douglas|MA|01516|5084742784|14438
i|100200405|Mikes System Test5|Douglas|MA|01516|5084742785|14438

Here's all i got now.

# open up the text file to parse.
open (IUD, "SomeTextfile.txt") or die "Can't open textfile: $!";
while (<IUD>)
{
   # create my hash

}
close IUD;


0
Comment
Question by:maddhacker24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 8221399
#!/usr/bin/perl
use strict;

open IUD, "SomeTextfile.txt" or die "Can not open textfile: $!\n";

chomp ($_) = <IUD>;
my @fields = split(/\|/);
my %hash;

while (<IUD>) {
  chomp;
  my $i=0;
  foreach split(/\|/) {
    $hash{$fields[$i]} = $_;
    $i++;
  }
}

close IUD;

However, I'm sure someone will come up with a 1 line map solution.
0
 
LVL 6

Accepted Solution

by:
holli earned 500 total points
ID: 8223326
use Data::Dumper;
use strict;

open (IUD, "t.txt") or die "Can't open textfile: $!";

# split fields line
my $line = <IUD>; chomp ($line);
my @f = split (/\|/, $line);
my @data = ();
my %data = ();

while ($line = <IUD>)
{
  # split value line
  chomp ($line);
  my @v = split (/\|/, $line);

  # create my hash
  my %h = map { $f[$_], $v[$_] } (0..$#v);

  # build array of hashes
  push @data, \%h;

  # or hash of hashes with SBID as field
  $data{$f[1]} = \%f;

}

print Dumper (\@data);
close IUD;
0
 

Author Comment

by:maddhacker24
ID: 8289022
Thank you very much!
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question