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

 
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

Your code would appear to do the job? :confused: What else are you looking for?

I could not understand exactly what you want, but maybe try like this :wink:

# cat file
3|32|1|4|2
5|4|7|67|12
23|10|45
# 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");
#./justdoit file
1 __LINE__ -> 1 2 3 4 32
2 __LINE__ -> 4 5 7 12 67
3 __LINE__ -> 10 23 45

regards
ygemici

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

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

I have to do the following

 
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

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

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

 
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

 
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

 
my $file_prv = $ARGV[1];

instead can we assign the sorted array there like

 
my $file_prv = @sorted;

Please find the attachment

I am sure if you explain to them how Unix can make their and your lives easier, they'll be willing to lend an ear. Otherwise install Cygwin in Windows to take advantage of a Unix-like environment on Windows.

tyler_durden

You can also get busybox.exe for a good-sized suite of UNIX tools in a 1-megabyte executable.