The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl array question from going through hash hankooknara Shell Programming and Scripting 2 07-29-2007 09:53 PM
hash,array and perl new2ss Shell Programming and Scripting 3 05-23-2007 11:30 AM
md5 hash a string or char array in SCO jcarter2333 High Level Programming 5 02-08-2007 04:09 PM
Hash within array, within hash, within array... jsmoriss Shell Programming and Scripting 1 07-10-2006 09:26 PM
getting data list into a hash array topcat8 Shell Programming and Scripting 5 03-09-2004 12:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-07-2008
kinmak kinmak is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 17
Read csv into Hash array?

Hi all experts,

May I know how to read a csv file and read the content in a hash in PERL?
Currently, I hard-coded and defined it in my code. I wanna know how to make up the %mymap hash thru reading the cfg.txt

====
csv file(cfg.txt):
888,444
999,333
===
#!/usr/bin/perl

my $file='p.csv';
my %mymap;
$mymap{888} = 444;
$mymap{999} = 333;

open(my $data, '<',$file) or die "Cannot open 'file'\n";
while (my $line=<$data>) {
my @column = split ",", $line;
if ($line =~ /Doc/) {
if (exists $mymap{$column[2]}) {
$column[3]=$mymap{$column[2]};}
foreach my $i (0..$#column) {
if ($i > 0) {
print ",",$column[$i];}
else {
print $column[$i];}
}}
else {
print $line}
}
exit;
  #2 (permalink)  
Old 05-07-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

Here is one way:
Code:
#!/usr/bin/perl

# @(#) p2       Demonstrate building hash (almost) directly from a file.

use warnings;
use strict;

my ($debug);
$debug = 0;
$debug = 1;

chomp( my (%hash) = map { split "," } my (@a) = <> );

foreach my $k ( keys %hash ) {
  print " key = $k, value = $hash{$k}\n";
}

exit(0);
Producing (with your data on file "data1"):
Code:
% ./p2 data1
 key = 888, value = 444
 key = 999, value = 333
See perl man pages or perldoc for details ... cheers, drl

Last edited by drl; 05-07-2008 at 09:59 PM.. Reason: Minor typo
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 04:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0