473,394 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Conversion of CSV file to hash table using perl

34
Hi All,

Please help me to convert csv file to a hash table with out using Text::CSV_XS , Text::CSV.

The CSV file is a dynamic file.

the fields are like this

Numb,Name,Class,Type,......

201,Bazaar,b2,53,..........

121,Raymond,y1,02,...

232,Robert,p1,04,...............



Here first line is name of fields and remaing are values to corresponding fields
Apr 21 '07 #1
9 16246
KevinADC
4,059 Expert 2GB
what have you tried so far?
Apr 21 '07 #2
pnsreee
34
what have you tried so far?
Im able to convert inot an array...but not possiable to hash
Apr 21 '07 #3
KevinADC
4,059 Expert 2GB
lets see the code you used to convert to an array....
Apr 21 '07 #4
pnsreee
34
lets see the code you used to convert to an array....

using this i can get each column into an array

my @myvar = `more +2 flat1.txt | cut -d "," -f $num` ;
Apr 21 '07 #5
KevinADC
4,059 Expert 2GB
thats not perl code. The example data you entered could be structured many different ways as a hash. You could read the file line by line and use the split() function to make a list of the fields, make one of the fields the hash key and the rest of the fields could be structured as an array or hash or just a single value. Assuming the first column/fields will be the hash keys:

Expand|Select|Wrap|Line Numbers
  1. my %hash = ():
  2. open(FH,'file.txt') or die "$!";
  3. while(<FH>){
  4.    chomp;
  5.    my @fields = split(/,/);
  6.    $hash{$fields[0]} = ??;
  7. }
close(FH);

and ?? is whatever the value should be for the rest of the columns/fields.
Apr 21 '07 #6
pnsreee
34
thats not perl code. The example data you entered could be structured many different ways as a hash. You could read the file line by line and use the split() function to make a list of the fields, make one of the fields the hash key and the rest of the fields could be structured as an array or hash or just a single value. Assuming the first column/fields will be the hash keys:

Expand|Select|Wrap|Line Numbers
  1. my %hash = ():
  2. open(FH,'file.txt') or die "$!";
  3. while(<FH>){
  4.    chomp;
  5.    my @fields = split(/,/);
  6.    $hash{$fields[0]} = ??;
  7. }
close(FH);

and ?? is whatever the value should be for the rest of the columns/fields.


Hi Kevin

Thanks for your quick replay.
Apr 23 '07 #7
Try that:

################################################## #####################
# Name : read_csv
#
# Description : Reads csv file into hash array
#
# In : csv file name, filed separation key (; in most cases)
#
# Out : hash array
################################################## #####################
sub read_csv {
my $csv_file = shift;
my $separator = shift;

my %hash=();
my @labels;
my @lines;
my $col_count = 0;
my $row = 0;
my ($line,$odd,$item,$data);

if (open(CSV,$csv_file)) {

while (<CSV>) {
# remove leading and trailing spaces
$_ = trim($_);
#skip emty lines
next if /^$/;
# copy next line
$line .= $_;

# if the line has an odd number of double qoutes it will concatenate the
# following lines until an od number of qoutes are encountered again.
if ((not $odd and /^[^"]*"[^"]*(?>[^"]*"[^"]*"[^"]*)*[^"]*$/) or # Regexp: test if even number of double qoutes
($odd and /^[^"]*(?>[^"]*"[^"]*"[^"]*)*[^"]*$/)) { # Regexp: test if odd number of double qoutes
$odd = 1;
# keep new line
$line .= " ";
next;
}
# reset column counter
$col_count = 0;
# remove carriage if any and linefeed from end
$line =~ s/\r?\n$//;
# split on separator if followed by an odd number of double qoutes
foreach $item (split /$separator(?=(?>[^"]*"[^"]*"[^"]*)*[^"]*$)/, $line, 100000) {

# remove leading and trailing spaces
$item = trim($item);
# remove last new line
$item =~ s/\s"$/"/;

# skip first line of labels
if ($row > 0) {
$hash{$row}{$labels[$col_count++]} = $item;
} else {
push (@labels,$item);
}

}
$row++;
undef $line;
undef $odd;
}
close (CSV);
} else {
print_log("$csv_file: $!");
die ("$csv_file: $!");
}
return (\@labels,\%hash);
}
Oct 1 '08 #8
nithinpes
410 Expert 256MB
Try that:

}
larsvegas,
There is no point in replying to a post which is more than an year old!! Also, use code tags while posting your code in future.
Oct 1 '08 #9
Icecrack
174 Expert 100+
that's the funniest thing i have seen in age's,

and yes use code tags, 2nd we don't post full code.
Oct 1 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Koncept | last post by:
Howdie Python folks! I am very new to Python ( 3rd day now ) and it has already earned its place as my fav. language to work in. I hope to continue, and I really would appreciate some good...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
6
by: Karl O. Pinc | last post by:
These seem to work. I'm posting them now before I'm done with them before I start messing them up with specifics to my database. Any comment is welcome. However, I've no intention of publishing...
1
by: kraj123 | last post by:
Hi, How to Find ,if already a environment variable is set in hash table in perl. Actually i want to check if a environmental variable in perl script, which is present in oracle database has...
3
crazy4perl
by: crazy4perl | last post by:
Hi All, I have some doubt related to xml. Actually I want to update a file which is in some format. So I am converting that file using Tap3edit perl module in a hash. Now I m trying to create a...
11
by: doraima29 | last post by:
Hi, I am a newbie at PERL and really wanted to understand how server-side programming really works and operates since I use at the workplace. I use ASP and wanted to learn more about server-side...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.