Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

how do I open each line of a text file into seperate arrays?

by husani (Initiate)
on May 01, 2001 at 01:07 UTC ( #76749=perlquestion: print w/replies, xml ) Need Help??
husani has asked for the wisdom of the Perl Monks concerning the following question:

I'm a newbie, forgive me if the question is braindead, but I've been trying for hours to no avail...

In my text file, there are 8 items on each line, seperated by pipes, i.e.:

foo|bar|more|and|hello|six|hello|world)

The number of lines in this text file will change on a regular basis.

I need to bring each line into an array ... and each item in the line as an item in the array.

I've spent time reading thru the Q&A on this site. I found out how to bring an ENTIRE text file into an array, but not how to split up the text file into lines, save each as an array with the array's content coming from the pipe-delimited data.

Am I making any sense here? :-) Thank you so much!

Replies are listed 'Best First'.
Re: how do I open each line of a text file into seperate arrays?
by turnstep (Parson) on May 01, 2001 at 01:26 UTC
    Here's a complete program. Basically the same as bbfu with a couple of enhancements. Adding a -1 to the split allows for null values to still be added, such as:
    foo|bar||||shazam
    and chomping the line removes the newline, otherwise it will end up in the last element of each array.

    my $myfile = "BigOldFile.psv"; ## pipe-separated values :) open(MYFILE, $myfile) or die "Could not open $myfile: $!\n"; my @lines = []; while(<MYFILE>) { chomp; push @lines, [split(/\|/, $_, -1)]; } ## This will show you the format: my $line=1; for (@lines) { print "Line $line: $_\n"; for (@$_) { print "*$_*\n"; ## Stars are to demonstrate null values } $line++; }
      open(my($fd),"./file") or die($!); @a=map{split('|',$_)}<$fd>; close($fd);
      my 2 cents :)


      lindex
      /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/
(bbfu) (AoA?) Re: how do I open each line of a text file into seperate arrays?
by bbfu (Curate) on May 01, 2001 at 01:11 UTC

    You could use an array of arrays:

    # open FILE while(<FILE>) { # The []'s make an anonymous array, so each element # of @lines is actually an array-ref. push @lines, [split /\|/, $_]; } print "Yep.\n" if $lines[0][0] eq 'foo'; # it will

    HTH.

    Upd: oops, fixed my array indexes. :-)

    Upd2: damn, what was I thinking? Changed @lines = [...] to proper push.

    Upd3: ARGH!!! Thanks for the eq catch, buckaduck. That's what I get when I try to rush a post... :-(

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

      print "Yep.\n" if $lines[0][0] == 'foo';
      I think you mean:
      print "Yep.\n" if $lines[0][0] eq 'foo';

      Just when you thought the updates were over...

      buckaduck

      Yet Another Use for SuperSplit!

      use SuperSplit; $AoA = supersplit_open( /\|/, $filename );
      ...and it's all there in $AoA.

      Jeroen
      "We are not alone"(FZ)

Re: how do I open each line of a text file into seperate arrays?
by Sherlock (Deacon) on May 01, 2001 at 01:22 UTC
    I'd try something like this:
    use strict; open (INPUT, "myFile.txt") || die "Couldn't open file: $!\n"; my @majorArray; while (<INPUT>) { my @minorArray = split('\|', $_); push @majorArray, \@minorArray; }
    This will store a reference to each array within one big array, called @majorArray. You can then access each array like this:
    my $arrayRef = $majorArray[0]; foreach my $temp (@{$arrayRef}) { print $temp,"\n"; }
    or you can get at all of the contents of every array like this:
    foreach my $item (@majorArray) { foreach my $subitem (@{$item}) { print $subitem,"\n"; } }
    If you're having trouble with references, you might want to try checking out How do I loop over a reference to an array? or simply look up references in the documentation. Or, even more simply, push the actual array into the larger array to give yourself a two-dimensional array.

    Hopefully, this'll help you get going. Good luck!

    - Sherlock
Re: how do I open each line of a text file into seperate arrays?
by Sifmole (Chaplain) on May 01, 2001 at 17:47 UTC
    And just one more way...
    open(IN, "<foo.txt"); # This line does the work. # <IN> reads lines from the file, feeding that list to the map. # split(/\|/, $_) creates a list of the the pipe-delimted elements # [ ] creates an anonymous array ref to that list. # Finally the map returns the resulting list of array refs. my @foo = map { [(split(/\|/, $_))] } <IN>; close(IN); # Just to prove that it worked. foreach (@foo) { print join(' -- ', @$_), "\n"; }
Re: how do I open each line of a text file into seperate arrays?
by husani (Initiate) on May 01, 2001 at 01:08 UTC
    that should say

    hello|world

    ignore that closing parenthesis....

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://76749]
Approved by root
help
Chatterbox?
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-07-18 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (393 votes). Check out past polls.

    Notices?