Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: Concatenating arrays fetched from different text files

by Laurent_R (Vicar)
on May 29, 2014 at 10:54 UTC ( #1087780=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Concatenating arrays fetched from different text files
in thread Concatenating arrays fetched from different text files

What do you mean by the 4th element of each array? From everything you said previously, you seemed to be willing to use all elements of the arrays. Or perhaps you mean that you need to use the 4th element of each line of the input files? Your requirement is not clear to me.


Comment on Re^3: Concatenating arrays fetched from different text files
Re^4: Concatenating arrays fetched from different text files
by thanos1983 (Monk) on May 29, 2014 at 19:42 UTC

    To: Laurent_R,

    Apologies for the confusion, I did not explain from the beginning that I want to retrieve the 4th element of each array. I thought it would not make a difference since I was not even aware of perlre.

    After spending some time I manage to understand, that my problem could be solved by applying that simple solution. Unfortunately since they are new to me I am not feeling that comfortable with them. Never the less is always fascinating to learn new capabilities of programming languages.

    I will update my sample files to clear the miss understanding about the 4th element that I am trying to read.

    Thank you for your time and effort reading and answering my question.

    Seeking for Perl wisdom...on the process...not there...yet!
      Well, if I understand you correctly, you really need the 4th element of each array, which means that you only really need the 4th line of each file. If this is correct, then don't go into the trouble of storing all your files into arrays, but simply store into a single array the 4th line of each file (and you can stop reading the file as soon as you've reached the 4th line). Your code will be far simpler, will execute faster and use much less memory.

      Assuming the files are passed as arguments to the script, you could have something as simple as this (untested):

      my @array; # I would use a better name if I knew the array's contents for my $file (@ARGV) { open my $FH, "<", $file or die "cannot open $file $!" while (<$FH>) { chomp and push @array, $_ and last if $. == 4; } } print "@array\n";
      Or did I misunderstand what you are after?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1087780]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-08-22 05:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (147 votes), past polls