Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

printing all elements of arrays

by drock (Beadle)
on Jul 13, 2004 at 00:59 UTC ( [id://373785]=perlquestion: print w/replies, xml ) Need Help??

drock has asked for the wisdom of the Perl Monks concerning the following question:

All, is there any way to print all the elements of any array instead of referencing each element like so [0] 1 2 ...etc? I was thinking something like * here is my code
use strict; open (FH,"/usr/local/bin/perld/derektapes") or die "cannot open FH: $! + \n"; my @a=(); my $i=0; my $ct=0; my $or_string=" or "; my $w_param="-w '"; my $b_param="barcode="; while (<FH>) { chomp $_; $a[$i]=$_; $i++; } print $w_param; print $b_param; print $a[0];

Replies are listed 'Best First'.
Re: printing all elements of arrays
by Fletch (Bishop) on Jul 13, 2004 at 01:06 UTC

    perldoc perlsyn, look for foreach. See also the entries for $" and $, in perldoc perlvar and perldoc -f join.

Re: printing all elements of arrays
by FoxtrotUniform (Prior) on Jul 13, 2004 at 01:06 UTC
    (I)s there any way to print all the elements of any array instead of referencing each element like so [0], [1], [2] ...etc?

    If you want to print out each element of an array on its own line,

    print join("\n", @array), "\n";
    is probably the easiest way to do it. If the extra "\n" offends you,
    print map {$_ . "\n"} @array;
    will do much the same thing, but it's less idiomatic (I think).

    --
    F o x t r o t U n i f o r m
    Found a typo in this node? /msg me
    % man 3 strfry

      I like print join("\n", @array, ""), myself.
      what does the print map state? what perldoc can I read about the map call?
        what does the print map state?

        Okay,

        print join("\n", @array), "\n";
        says "Stick a newline between each element in @array, return that as a string, print it, and print a newline at the end." On the other hand,
        print map {$_ . "\n"} @array;
        says "Build a list whose elements are the elements of @array with "\n" appended to them, then print that list."

        what perldoc can I read about the map call?

        You can read about any function with perldoc -f func_name.

        --
        F o x t r o t U n i f o r m
        Found a typo in this node? /msg me
        % man 3 strfry

        perldoc -f map.

        map is a list-transformer; you give it a block of code and a list, and it applies the code to each element of the list, and returns a list made up of what the code returned for each element.

        You can read perlfunc to find out about map. Also, the easiest way to print all the elements of an array is:

        print "@array\n";

Re: printing all elements of arrays
by graff (Chancellor) on Jul 13, 2004 at 01:31 UTC
    This part of your code:
    while (<FH>) { chomp $_; $a[$i]=$_; $i++; }
    Can be rendered in a variety of briefer ways, briefest of which is probably:
    @a = <FH>; # diamond operator in "list" (or array) context # will read all input records into array elements chomp @a; # chomp can work on a list
    If there are other things you want to do while assigning input records to an array, you can keep the while loop, but without using the array index variable ($i):
    while (<FH>) { chomp; # $_ is the default arg to chomp; # ... do other things to $_ if necessary ... push @a, $_; # add a new element at end of @a }
    In terms of printing the array elements as a "joined set", you should just play with different things to see how they work -- try a few command lines like this:
    perl -e '@a=qw/one two three four/; print @a,$/' perl -e '@a=qw/one two three four/; print "@a$/"' perl -e '@a=qw/one two three four/; print join(" ", map { "foo=$_" } +@a), $/'
    R some more FMs and have fun with it.
Re: printing all elements of arrays
by Gunth (Scribe) on Jul 13, 2004 at 04:43 UTC
    $, = "\t"; print @array; #or print shift . "\t" while @array; #or print $_ . "\t" for @array; #or print map { $_ . "\t" } @array; #or print join("\t", @array);
    TMTOWTDI. I would chose the first way if it were the only print in that scope. If not, I would chose the second.
    -Will
Re: printing all elements of arrays
by pbeckingham (Parson) on Jul 13, 2004 at 03:30 UTC
Re: printing all elements of arrays
by ysth (Canon) on Jul 13, 2004 at 01:23 UTC
    Perl has lots of different ways to handle arrays. Can you explain better what output you are actually looking for?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://373785]
Approved by FoxtrotUniform
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found