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

Printing an element of a list not an array

by raghuprasad241 (Beadle)
on Aug 25, 2016 at 15:13 UTC ( [id://1170445]=perlquestion: print w/replies, xml ) Need Help??

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

GMAN all,

GMAN (Good Morning, Afternoon, Night) depending on where you are :-)

Is it possible to print individual elements of a list with out putting them into an array ? I think putting the list into an array is a wastage of memory if you are just trying to print it. For e.g. I am trying to do below with no success

perl -e 'use v5.10; say (localtime)[1]' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

Am I missing something simple ?

Thanks!
A Monk!

Replies are listed 'Best First'.
Re: Printing an element of a list not an array
by Marshall (Canon) on Aug 25, 2016 at 16:00 UTC
    With say or print, there is an optional file handle,right after the "print", print $handle "something"; Perl is confused about what you mean if there is just, (localtime)[1] right after the "print". All of these statements below do essentially the same thing.
    my $x = (localtime)[1]; print "$x\n"; print "".(localtime)[1],"\n"; print ((localtime)[1],"\n"); print STDOUT (localtime)[1],"\n";
Re: Printing an element of a list not an array
by choroba (Cardinal) on Aug 25, 2016 at 20:21 UTC
    It seems no one has mentioned another common way of avoiding this trap:
    perl -E 'say +(localtime)[1]'

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      I was surprised that this worked, but it did:
      my @array = (32, "something","something01","01something"); print scalar @array."\n"; #prints 4 print +(@array)[0], "\n"; #prints 32 print +(@array)[1], "\n"; #prints something print +(@array)[2], "\n"; #prints something01 print +(@array)[3], "\n"; #prints 01something
      An issue that I see is how Perl handles math on strings and potential confusion resulting from that:
      my $test = '01xxx'; $test += 1; print "01xxx + 1 is: $test\n"; #prints 2
      I do most of my work on Windows now and printing to the console in any form is such an incredibly "expensive" execution wise operation that I prefer the print "".function; option. The string concatenation is meaningless in the overall scheme of things.
      I was going to mention that, but had forgotten by the time I had completed my post. I'm glad you've added it to the thread. Cheers, choroba!
Re: Printing an element of a list not an array
by Anonymous Monk on Aug 25, 2016 at 15:21 UTC
    perl -e 'use v5.10; say ((localtime)[1])'

      Note that you can bypass the whole use statement by using -E:

      perl -E 'say ((localtime)[1])'
      Nice and thank you!

      However, in one of the examples of intermediate perl there is a section in the package that don't use surrounding circular braces. Any idea why ?
      See below code its working fine with out any braces surrounding the list, does it have to do anything with the return?

      { package MyDate; use vars qw($AUTOLOAD); use Carp; my %Allowed_methods = qw( date 3 month 4 year 5 ); my @Offsets = qw(0 0 0 0 1 1900 0 0 0); sub new { bless {}, $_[0] } sub DESTROY {} sub AUTOLOAD { my $method = $AUTOLOAD; $method =~ s/.*:://; unless( exists $Allowed_methods{ $method } ) { carp "Unknown method: $AUTOLOAD"; return; } my $slice_index = $Allowed_methods{ $method }; return (localtime)[$slice_index] + $Offsets[$slice_index]; } } MyDate->import; # we don't use it my $date = MyDate->new(); print "The date is " . $date->date . "\n"; print "The month is " . $date->month . "\n"; print "The year is " . $date->year . "\n";

        This is because when using print or say, there is ambiguity in what you're passing. Without the parens:

        perl -w -E 'say (localtime)[1]' say (...) interpreted as function at -e line 1.

        say like print are both functions. perl just makes it convenient that you can omit a function's parens in most situations. say believes that the opening parens belongs to itself, like this: say( localtime )[1] ;, which is wrong. So if you look at it like this: say( (localtime)[1] );, it may become clearer as to what's happening. In most other cases, you can eliminate the parens:

        my $x = (localtime)[1];

        or

        return (localtime)[1];
Re: Printing an element of a list not an array
by raghuprasad241 (Beadle) on Aug 25, 2016 at 16:16 UTC
    Thank you very much every one!
    All these explanations helped me better understand workings of perl. I appreciate the valuable time you spent on explaining this to me.

    Raghu

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found