LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-24-2014, 11:15 PM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
Creating HoH using perl


Hi,

I want to read file in a hash and acess each field using PERL

Could you please help me with creating HoH using the below data.


PHP Code:
Sl.no,Path,Category1,Remarks
1
,/home/jags1984/sample_c_code/PE-MultiClientCalc/*, Multi Client Calc ,
2,/home/jags1984/sample_c_code/PE-MultiClientCalc/pmm_mcpestat/*,Status,
3,/home/jags1984/sample_c_code/PE-StandardCalc/*,Standard Calc,
4,/home/jags1984/sample_c_code/PE-MultiClientCalc/pmm_utilib/*,Utility Library,
5,/home/jags1984/sample_c_code/PE-StandardCalc/pmm_pestat/*,Status, 
 
Old 11-25-2014, 12:21 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,645

Rep: Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501
what have you written so far?
 
Old 11-25-2014, 12:47 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
This is not exactly what I wanted, I want to access each field


Quote:
my %row;
open(DATA,"< $Filename");

my @fields = split(/,/,<DATA>) ;

chomp @fields;

while (<DATA>) {
chomp;


@row{@fields} = split(/,/);

}


foreach $key (sort keys %row){

print "$row{'Category1'} \n";
}

Last edited by jags1984; 11-25-2014 at 12:50 AM.
 
Old 11-25-2014, 02:15 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,645

Rep: Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501
Code:
@row{@fields} = split(/,/);
you save only the last processed line, you always overwrite the previous (instead of saving the new values)
Code:
foreach $key (sort keys %row){
    print "$row{'Category1'} \n";
}
you made a loop on the keys, but $keys is not used inside the loop, just a static value is printed.
You need to start your script with
Code:
#!/usr/bin/perl -w
use strict;
 
Old 11-25-2014, 02:49 AM   #5
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
you save only the last processed line, you always overwrite the previous (instead of saving the new values)


Can you please tell me how to do that ?

I am still a beginner in perl.

Last edited by jags1984; 11-25-2014 at 02:54 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
creating perl dictionary deeptic Programming 4 09-09-2009 02:47 AM
Creating a Perl Program with options HyperTrey Programming 15 12-18-2008 08:29 PM
creating environment variables in perl iceman_san Linux - Newbie 7 08-11-2007 08:56 PM
Creating AT jobs with perl Elguapo Programming 3 07-23-2007 06:43 PM
creating userid's in perl BBQ_Matt Programming 1 01-28-2004 01:23 PM


All times are GMT -5. The time now is 12:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration