Perl: Reading data from other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Reading data from other file
# 1  
Old 04-21-2010
Perl: Reading data from other file

Hi, I am writting some perl scripts for daily backup process. In which I want to pass some data/referance from another txt file. Text file contains only one column and multiple rows. I want to pass this data to variables of another perl script.

e.g.
Refdoc.txt file contains data as:
perl1
perl2
perl3
perl4

Now I want to assign this data to another perl script to a variales as:
$var1 = 'perl1'
$var2 = 'perl2'
$var3 = 'perl3'
$var4 = 'perl4'

How can achieve this task in perl?

Waiting for reply.

Regards
# 2  
Old 04-21-2010
Use the following code
Code:
open FH,"< Refdoc.txt" or die "Can't open file : $!\n";
my @array = <FH>;

for (my $i = 0; $i <= $#array; $i++)
{

         $var{$i} = $array[$i];
}

foreach $key (keys %var)
{
    print "$var{$key}";
}


Last edited by thillai_selvan; 04-21-2010 at 06:40 AM..
# 3  
Old 04-21-2010
You could actually use an array for this kind of stuff...

Code:
$a= 0;
while(<DATA>)  {
        $var[++$a] = $_;

        print $var[$a];
}
__DATA__
perl1
perl2
perl3
perl4

# 4  
Old 04-21-2010
thanks thillai, it worked and solved my problem
thanks again..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : reading data from dumper variable

Hi team, # PERL I have Dumper variable in perl and containing the below data and trying to fetch value and name from the reference variable. $VAR1 = { 'retainSysIds' => 'true', 'variables' => , 'name' => , ... (4 Replies)
Discussion started by: giridhar276
4 Replies

2. 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

3. Shell Programming and Scripting

Reading data from file using awk

I have a file as below. It contains two data sets separated by >. I want to pipe each data set to another program called psxy. How can I get the different records Have started doing as follows but it only passes the first data set awk 'BEGIN {RS=">"};{print $0}' p.dat cat p.dat... (12 Replies)
Discussion started by: kristinu
12 Replies

4. Shell Programming and Scripting

reading data from a file to an array

I need some help with this code below, i doesnt know why it will run twice with my function, but my function only got if else, any other way that can read line and put into array? while read line; do read -A array <<<$line n=${#array} for ((i=1;i<$n;i++)); do print... (1 Reply)
Discussion started by: gavin_L
1 Replies

5. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

6. Shell Programming and Scripting

PERL: reading 2 column data into Hash file

I am trying to read in a 2 column data file into Perl Hash array index. Here is my code. #!/usr/bin/perl -w use strict; use warnings; my $file = "file_a"; my @line = (); my $index = 0; my %ind_file = (); open(FILE, $file) or die($!); while(<FILE>) { chomp($_); if ($_ eq '') { ... (1 Reply)
Discussion started by: subhap
1 Replies

7. Shell Programming and Scripting

reading data from .ini file

Hi, I have a sample data file that contains name-value pairs in it. For example: name1=value1 name2=value2 name3=value3 ... ... ... My concern is 1. How to get values of name1, name2, name3 using korn shell script? 2. I don't want to access each varible using $name1,$name2, $name3... (2 Replies)
Discussion started by: vinna
2 Replies

8. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies

9. Shell Programming and Scripting

Reading data into muti-dimentional array - in perl

Just want to learn how these are read into array but I don't seem to get it right what do I go wrong? Below is the sample Thanks input 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 #!/usr/bin/perl open (InFILE,"input"); while (<InFILE>) { @ar = split ; (5 Replies)
Discussion started by: zap
5 Replies
Login or Register to Ask a Question