perl -write values in a file to @array in perl | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

perl -write values in a file to @array in perl

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-04-2008
meghana meghana is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 28 May 2008, 2:57 PM EDT
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
perl -write values in a file to @array in perl

Hi

can anyone suggest me how to write a file containing values,... say

19
20
21
22
..
40

to an array @array = (19, 20, ... 40)

-- Thanks
Sponsored Links
    #2  
Old 02-04-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts

Code:
open(FILE, "<", "file");

while(<FILE>) {
  chomp;
  push(@arr, $_);
}

close(FILE);

Sponsored Links
    #3  
Old 02-04-2008
meghana meghana is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 28 May 2008, 2:57 PM EDT
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
thanks for the reply.. but it dint work... do u have any further suggestions???
    #4  
Old 02-04-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by meghana View Post
thanks for the reply.. but it dint work... do u have any further suggestions???
Why it didn't work ?

What is the expected output ?

Do you want to print the array elements ? If so, add the following snippet at the end


Code:
foreach(@arr) {
  print "array element: $_\n";
}

Sponsored Links
    #5  
Old 02-04-2008
meghana meghana is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 28 May 2008, 2:57 PM EDT
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
i dont know but.. this one worked for me...

open(FILE, "<", "tmpfile");
my @values = <FILE>;
close(FILE);

i just wanted to write the values of a file into @array... but still.. ur example helped me as a start point... appreciate it.. thanks again...
Sponsored Links
    #6  
Old 02-04-2008
meghana meghana is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 28 May 2008, 2:57 PM EDT
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
can you help me on this... i try to print them($count, $last_rec) in two different lines but they get printed in one line if i use this script.....

open (fh,">>","file");
print fh "$count";
print fh "$last_rec";
close(fh);

can you suggest any solution for this...thanks
The Following User Says Thank You to meghana For This Useful Post:
meghana.ghana (02-16-2011)
Sponsored Links
    #7  
Old 02-04-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by meghana View Post
i dont know but.. this one worked for me...

open(FILE, "<", "tmpfile");
my @values = <FILE>;
close(FILE);

i just wanted to write the values of a file into @array... but still.. ur example helped me as a start point... appreciate it.. thanks again...
Well, this is also one of the way to do that.

The other one I had posted should also work
The Following User Says Thank You to matrixmadhan For This Useful Post:
meghana.ghana (02-16-2011)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search and replace a array values in perl arindam guha UNIX for Advanced & Expert Users 1 05-03-2013 09:33 PM
PERL : Read an array and write to another array with intial string pattern checks irudayaraj Shell Programming and Scripting 2 11-05-2011 08:11 AM
How to map the values of an array in perl? vanitham Shell Programming and Scripting 3 05-24-2011 07:18 AM
Greping array values in Bash like Perl paragkalra Shell Programming and Scripting 0 08-10-2010 02:38 PM
perl: Assigning array values.. looza Shell Programming and Scripting 4 07-15-2008 08:08 AM



All times are GMT -4. The time now is 03:45 PM.