Reading values in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading values in perl
# 1  
Old 02-11-2010
Reading values in perl

Hi whats the easiest way to read data from a feed file?

sample data in a file called data.txt:
Code:
name = varun
ip = '23.43.123.2'
address = "asd, blah blah blah ..... @#!$%$#%"

i want to use this data in a perl script.
I thought of initially reading it line by line n then cutting fields out but that didn't seem to work well with the single quotes and all....
# 2  
Old 02-11-2010
Something like this:
Code:
$ cat data.pl
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my %conf = map {
    chomp;
    my @a = split / = /;
    $a[1] =~ s/['"]//g;
    $a[0] => $a[1];
} <DATA>;
print Data::Dumper->Dump( [ \%conf ], [qw/conf/] );

__DATA__
name = varun
ip = '23.43.123.2'
address = "asd, blah blah blah ..... @#!$%$#%"

$ perl data.pl
$conf = {
          'ip' => '23.43.123.2',
          'name' => 'varun',
          'address' => 'asd, blah blah blah ..... @#!$%$#%'
        };

# 3  
Old 02-11-2010
thanks man...
in the same situation,
supposing the data was present in a file called data.txt
how would i pull out the data then???
# 4  
Old 02-11-2010
By using open to, y'know, "open" the file and reading from the resulting file handle.
# 5  
Old 02-11-2010
isn't there any way to read the entire file in one shot rather than record wise.... it's a little confusing... could u plzzzzzzzzzzzzzzz show me an example... thanx.... Smilie
# 6  
Old 02-11-2010
Let me ask you a question: if you don't know how to use the open function, how are you going to make anyone believe you thought up the map?

And please use proper English instead of that script-kiddy "plz" and "thanx".
# 7  
Old 02-11-2010
first up....
im not trying to convince anyone that i thought up the map....
all im trying to do is understand and learn how this works..

I have used the open function before like this:

Code:
if (open(INFILE, $locationoffile)) {
	@fh = <INFILE>;
	close(INFILE);
} #[else.. whatever....]

after opening it up like this, i count the number of lines in the array and then run two loops to pick out one line(record) at a time....
ALL I WANTED to know was if there was another way to use the data dumper which would help me pick up data in a better way
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading and Comparing values of file

Hi gurus.. Am reading a file, counting number of lines and storing it in a variable. Then am passing that variable into If loop for comparision, if the number of lines are greater than 1000 it should split a file if not it should send the file name to archive folder.. but when i execute the... (4 Replies)
Discussion started by: azherkn3
4 Replies

2. Shell Programming and Scripting

Reading off values from a large file

Hi, I have a large output file (star.log), with many lines of the following type *** T vavg unburnt: 723.187 / burnt: 2662.000 What I would like to do is pick the values 723.187 and 2662.000 and What I've got so far is awk '/unburnt:.*burnt:/{Tu=$6;Tb=$NF}END{print Tu, Tb}'... (6 Replies)
Discussion started by: lost.identity
6 Replies

3. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

Reading values from sql query

I have sql query in shell script. select distinct account_no from adj order by account_no; This query returns account number daily.Sometimes it may return 90 rows sometime it may return 1 row only and someday it may return 0 rows I am storing the output of this query in sql_output.txt. ... (5 Replies)
Discussion started by: rafa_fed2
5 Replies

5. Shell Programming and Scripting

Reading multiple values in while loop

I'm having trouble with a simple piece of code. IFS=, echo "1,2,3,4,5,6,7,8" | while read x y do echo "x=$x" echo "y=$y" done I'm hoping for x=1 y=2 x=3 y=4 . . . but I'm getting x=1 (3 Replies)
Discussion started by: sabbata
3 Replies

6. Shell Programming and Scripting

reading a script and summing some values

I need help reading and summing some values in a file that looks like the following. This is an Oracle trace file. Oracle has as utility to do this,but it doesn't work properly unless my sql statement is done. I want to read the file and sum up some values to let me know how the query/job is... (0 Replies)
Discussion started by: guessingo
0 Replies

7. Shell Programming and Scripting

Reading values from a file

Hi I have a file in the following format AFUE 0. AOXI 0. VFUE 100.0 VOXI 274.601 TFUE 298. TOXI 2229.544 TMAX 2400. What I want to do is write a bash script, that use either perl/awk or sed to read the number after VFUE and VOXI (which is 100.0 and... (1 Reply)
Discussion started by: lost.identity
1 Replies

8. UNIX and Linux Applications

Reading values from the command line

Hi I want to give the user the choice of whether or not they want to include a certain option when they run the script. This is my getops: while getopts " s: d: r f: e h " option do case $option in f ) dsxfile="$OPTARG";; d ) dbname="$OPTARG";; s ) dsn="$OPTARG";; r )... (0 Replies)
Discussion started by: ladyAnne
0 Replies

9. Shell Programming and Scripting

reading values into an array

I have this portion of a script and it works: base_config="A B C D E" for field in $base_config do var=`echo $field` ((i=$i+1)) done I get an array of var=A, var=B, and so on. What if I have this example with white space and want to put it into an array while... (1 Reply)
Discussion started by: djehresmann
1 Replies

10. UNIX for Dummies Questions & Answers

Reading from a file and assigning values

HI I have something like this in a file ABC = 1 DEF = 2 GHI = 3 JKL = 4 MNO = 5 QRS = 6 TUV = 7 I need to assign ABC to V_abc (that is to a variable) GHI to V_ghi (that is to another variable) TUV to say V_tuv ... (6 Replies)
Discussion started by: ssuresh1999
6 Replies
Login or Register to Ask a Question