Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 285661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:33:59+00:00 2026-05-12T05:33:59+00:00

I came across this syntax while reading a script. I am not sure what

  • 0

I came across this syntax while reading a script. I am not sure what is the use of square brackets.

push @data, [ split //, $line ]; #printing this array gives crap values

Or to put into other words what is the difference between the above and the following?

push @data, (split//, $line); #printing this gives actual values

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:34:00+00:00Added an answer on May 12, 2026 at 5:34 am

    The code:

    push @data, (split//, $line);
    

    pushes all items on the current line into @data

    and

    push @data, [split //, $line]; 
    

    Pushes a reference to an anonymous array containing those items into @data

    If you’re only ever processing one value of ‘$line’ its probably more effective to use the former*1 , however, if you are processing a file that contains multiple lines and you want to differentiate between the lines the content is on, the latter is more effective.

    Consider:

    my @data; 
    while( my $line = <$fh> ){
      push @data , ( split //,  $line ); 
    }
    use Data::Dumper; 
    $Data::Dumper::Indent = 0; 
    $Data::Dumper::Terse = 1;
    print Dumper( \@data ); 
    

    This will yield all of the bytes read in as separate characters, a single
    array containing them all, i.e.:

    [ "a", "b" , "c" , "\n", "d", "e", "f" ]
    

    When this instead will do something entirely different:

    my @data; 
    while( my $line = <$fh> ){
      push @data , [ split //,  $line ]; 
    }
    use Data::Dumper; 
    $Data::Dumper::Indent = 0; 
    $Data::Dumper::Terse = 1;
    print Dumper( \@data ); 
    

    And will instead group lines like so:

    [ [ "a", "b", "c" , "\n" ], [ "d" , "e", "f" , "\n" ] ]
    

    So you can later programmatically traverse it easier.

    Note:

     push @data, ( split  //, $line ); 
    

    and

      push @data, split //, $line; 
    

    Are equivalent.

    Also,

    my @other  = ( 1,2,3 );
    push @data, @other ;
    

    and

    push @data, 1,2,3; 
    

    are equivalent.

    From perldoc -f push

    push ARRAY,LIST
    
    Treats ARRAY as a stack, and pushes the values of LIST onto the end of ARRAY.  The length of ARRAY increases by the length of LIST.  Has the same effect as
    
               for $value (LIST) {
                   $ARRAY[++$#ARRAY] = $value;
               }
    
    but is more efficient.  Returns the number of elements in the array following the completed "push".
    

    *1: actually, tbf, anyone with half a brain would probably want @data = split //, $line

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 157k
  • Answers 157k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's stored in the Attributes of the MediaElemet, which are… May 12, 2026 at 11:00 am
  • Editorial Team
    Editorial Team added an answer No. All requests has to go through a controller. I… May 12, 2026 at 11:00 am
  • Editorial Team
    Editorial Team added an answer TypeMock is very powerful. I guess it can do it.… May 12, 2026 at 11:00 am

Related Questions

A while ago I had asked how to make a collapsible comment box like
I came across this line in some code and can't find the syntax defined
I came across this statement as code, on a programming puzzle web site. (i.
I just came across this code and a few Google searches turn up no
I recently came across some code that looked like: if(sizeof(var,2) == 4) { ...

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.