from text file to array in shortest way

Discussion in 'Perl Misc' started by Brian Wakem, Nov 7, 2005.

  1. Brian Wakem

    Brian Wakem Guest

    ngoc wrote:

    > my @books_array = ();
    > open(FILE, '<books_file.txt') or croak 'Can open file';
    > while (<FILE>){
    > push @books_array, $_;
    > }
    > Other shorter ways than above?


    open......
    my @books_array = <FILE>;


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Nov 7, 2005
    #1
    1. Advertising

  2. Brian Wakem

    John Bokma Guest

    ngoc <> wrote:

    > my @books_array = ();


    = () is not needed.

    > open(FILE, '<books_file.txt') or croak 'Can open file';
    > while (<FILE>){
    > push @books_array, $_;
    > }
    > Other shorter ways than above?


    use File::Slurp;

    my @books_array = read_file( 'filename' );

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Nov 7, 2005
    #2
    1. Advertising

  3. Brian Wakem

    ngoc Guest

    my @books_array = ();
    open(FILE, '<books_file.txt') or croak 'Can open file';
    while (<FILE>){
    push @books_array, $_;
    }
    Other shorter ways than above?
     
    ngoc, Nov 7, 2005
    #3
  4. ngoc wrote:
    > my @books_array = ();
    > open(FILE, '<books_file.txt') or croak 'Can open file';
    > while (<FILE>){
    > push @books_array, $_;
    > }
    > Other shorter ways than above?


    Hi,
    I'm not sure if this qualifies as an entry but one way would be to use the
    diamond operator and specify a file name on the command-line. The down-side
    with this approach is that you cannot, to my knowledge, change the error
    message displayed by Perl. If the file canot be opened, you're program will
    not even run because the file is thought of as STDIN and Perl fails to get
    it. Or this is how i think of it from the user point of view.

    Also, I think croak is not used by default so error messages may be reported
    differently, that is the exact line where it died rather than giving the
    callers line number.

    The code would be:

    my @books_array = <>;

    If you want a custom error message, you'll have to open the file explicitli
    I guess. Or maybe a die-handler can also do the croaking, though that's at
    least one extra line and a bit of a hack, too. The thing I 've found
    confusing is that die-handlers can get called even inside an eval block,
    where a death is not a fatal thing (the program can still continue).

    I wonder how we measure shortness in this particular case? A one-line loop
    like:

    push @books_array, $_ while <FILE>;

    does exactly the same thing as your while loop but saves a few braces. But
    then again, this is not good for the future. If you'll need to do more than
    one thing inside the while loop, you can either try tricks with the comma
    operator or give up and make it a more regular while. Similarly, if
    strictness is not required you could just use the array without mentioning
    it first.

    I'm rather new to Perl still but thought I'd give this a try.

    --
    With kind regards Veli-Pekka Tätilä ()
    Accessibility, game music, synthesizers and programming:
    http://www.student.oulu.fi/~vtatila/
     
    Veli-Pekka Tätilä, Nov 8, 2005
    #4
  5. Brian Wakem

    Guest

    ngoc wrote:
    > while (<FILE>){
    > push @books_array, $_;
    > }
    > Other shorter ways than above?


    Use IO::All
    my @books = io($file) -> slurp;
     
    , Nov 8, 2005
    #5
  6. ngoc <> wrote in news:436fc98d$:

    > my @books_array = ();
    > open(FILE, '<books_file.txt') or croak 'Can open file';
    > while (<FILE>){
    > push @books_array, $_;
    > }
    > Other shorter ways than above?


    use Tie::File;
    tie my @books_array, 'books_file.txt' or die "Can't open: $!";

    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
     
    Eric J. Roode, Nov 12, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robin Wenger
    Replies:
    11
    Views:
    3,744
  2. Sven Fischer
    Replies:
    6
    Views:
    689
  3. Ara.T.Howard
    Replies:
    3
    Views:
    121
  4. Iñaki Baz Castillo

    Extracting the shortest string from an array

    Iñaki Baz Castillo, Mar 3, 2011, in forum: Ruby
    Replies:
    11
    Views:
    449
    Robert Klemme
    Mar 4, 2011
  5. ardi
    Replies:
    1
    Views:
    507
    Victor Bazarov
    Jul 2, 2012
Loading...

Share This Page