Visit Our UNIX and Linux User Community


perl: Read array from a flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: Read array from a flat file
# 1  
Old 11-21-2011
perl: Read array from a flat file

Hello Guru's

I want to read an array into a flatfile

Please let me know how to do the same
So far this the below code


Code:
 
use strict;
use warnings;
open (my $data , '<', $ARGV[0])|| die "could not open $ARGV[0]:\n$!";
my @array=(<$data>);
my @sorted=sort {(split(/\|/,$a))[2]<=>(split(/\|/,$b))[2]} @array;
print @sorted;

Please let me know how to open the array from a file
# 2  
Old 11-21-2011
Your code would appear to do the job? Smilie What else are you looking for?
# 3  
Old 11-21-2011
Quote:
Originally Posted by Pratik4891
Hello Guru's

I want to read an array into a flatfile

Please let me know how to do the same
So far this the below code


Code:
 
use strict;
use warnings;
open (my $data , '<', $ARGV[0])|| die "could not open $ARGV[0]:\n$!";
my @array=(<$data>);
my @sorted=sort {(split(/\|/,$a))[2]<=>(split(/\|/,$b))[2]} @array;
print @sorted;

Please let me know how to open the array from a file
I could not understand exactly what you want, but maybe try like this Smilie
Code:
# cat file
3|32|1|4|2
5|4|7|67|12
23|10|45

Code:
# cat justdoit
#!/usr/bin/perl
$n=0;
open("mydata","<$ARGV[0]")||die("Unable to open file -> '$!'");
while(<mydata>){chomp;$n++;
@splitted = split(/\|/);
@sorted = sort { $a <=> $b } @splitted;
print "$n __LINE__ -> ", "@sorted\n";}
close("mydata");

Code:
#./justdoit file
1 __LINE__ -> 1 2 3 4 32
2 __LINE__ -> 4 5 7 12 67
3 __LINE__ -> 10 23 45

regards
ygemici
# 4  
Old 11-22-2011
Guys ,

Thanks a lot for replying .I know the requirement is weird

The point is after sorting two files I have to compare files to each other

Is there any way I can do like this

Code:
 
open my $fh1, '<', @sorted or die "Can't open $file1: $!";

I have to do the following

Code:
 
while (<$fh1>){ 
chomp; 
my @rec = split /\|/; 
my $key = $rec[2]; 
$save{$key}->{'data'} = $_; #Save current record in hash 
$save{$key}->{'flag'} = 'D';
}

so I need to read the sorted array into $fh1

Thanks a lot for your support
# 5  
Old 11-22-2011
Quote:
Originally Posted by Pratik4891
... after sorting two files I have to compare files to each other
...
If all you want to do is sort and compare two files, then simple Unix commands could do the job.
Maybe posting your representative data could give someone an idea of what you are trying to accomplish.

tyler_durden
# 6  
Old 11-22-2011
Thanks Tyler .

The point is I can't use unix(although I loveeeee it ) because people who pay me asked me to use only perl.
So you guys are my savior

The code is attached

You can see that in the below code 3 files are opening

Code:
 
open my $fh1, '<', $file1 or die "Can't open $file1: $!";
open my $fh2, '<', $file2 or die "Can't open $file2: $!";
open my $fh3, '>', $file3 or die "Can't open $file3: $!";

I want to open the sorted file instead of file1,file2
Is there a way I can use

Code:
 
open my $fh1, '<', @sorted or die "Can't open $file1: $!";

Thanks a ton for your help

P.S.
In the original code we are using

Code:
 
my $file_prv = $ARGV[1];

instead can we assign the sorted array there like

Code:
 
my $file_prv = @sorted;

# 7  
Old 11-22-2011
Please find the attachment

Previous Thread | Next Thread
Test Your Knowledge in Computers #112
Difficulty: Easy
macOS was not the largest Unix largest installed base in 2017.
True or False?

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Create a bash array from a flat file of whitespaces only.

Hi guys and gals... MacBook Pro. OSX 10.13.2, default bash terminal. I have a flat file 1920 bytes in size of whitespaces only. I need to put every single whitespace character into a bash array cell. Below are two methods that work, but both are seriously ugly. The first one requires that I... (7 Replies)
Discussion started by: wisecracker
7 Replies

2. UNIX and Linux Applications

Perl Script to read an excel file into an array and search in the UNIX directories

Hi, I want the Perl script with versions 5.8.2 and 5.8.5 starting with #!/usr/bin/perl The Perl program should read the excel file or text file line by line and taking into an array and search in the UNIX directories for reference file of .jsp or .js or .xsl with path .The Object names... (2 Replies)
Discussion started by: pasam
2 Replies

3. Shell Programming and Scripting

Read from Multiple character delimited flat file

Hello Guys I have a multiple character delimited flat file "|~|". when I tried to read the data the "|" character also coming Example I/P file 9882590|~|20111207|~|K03501000063005574033961|~|K|~| Command to get the second column I used awk -F"|~|" ' {print $2}' ... (2 Replies)
Discussion started by: Pratik4891
2 Replies

4. Shell Programming and Scripting

Read flat file upto certain number of columns

Hello Guys Please help me with the below issue I want to read a flat file source upto certain number of columns Say my flat file has 30 columns but I want to read upto 25 columns only How come the above issue can be addressed? Thanks a lot!!!! (1 Reply)
Discussion started by: Pratik4891
1 Replies

5. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

6. Shell Programming and Scripting

To read a flat file containing XML data

I have a file something like this:aaaa.xml content of the file is 0,<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <storeInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <s> <BRANCH_NO>3061</BRANCH_NO> <BRANCH_NAME>GREEN EXPRESS</BRANCH_NAME> ... (4 Replies)
Discussion started by: kmanivan82
4 Replies

7. Shell Programming and Scripting

How to read the first column in a flat file with ~ as delimiter

I have one flat file like below id1~col~batch1 id2~col2~batch2 id3~col3~batch3 I need to read the first column one by one and I need to write one db2 query based on that column1 Like for (i=0;i<=10;i++) do insert into table column (con_id) values (select column from table where... (4 Replies)
Discussion started by: siri_886
4 Replies

8. Shell Programming and Scripting

how to read fixed length flat file....

Hi Gurus, Thanks in advance... I am new to writing shell scripting and help me out reading a flat file with fixed length. I have a fixed length flat file with storename(lenth 6) , emailaddress(lenth 15), location(10). There is NO delimiters in that file. Like the following str00001.txt... (2 Replies)
Discussion started by: willywilly
2 Replies

9. Shell Programming and Scripting

Perl: Read directories and assign to array

I would like to read directories and assign to an array where the user can select a directory from the output. For example, a dir list will populate with a number assigned to each dir. I would like to ask the user to select the dir they want. TIA, I don't know what this would be called so I... (2 Replies)
Discussion started by: man
2 Replies

10. Shell Programming and Scripting

How to read lines from a flat file

i have some commands written line by line in one flat file i have to read each linefrom this file(file name passed as parameter) and then i have to execute this How can i do it? (2 Replies)
Discussion started by: bihani4u
2 Replies

Featured Tech Videos