-4

Ok I realize my original question was really bad. Here goes take 2. Our goal is to replicate the behaviour the folowing commands in Perl using only "splice".

my @arr = qw (A B C D);
@arr = ();
#or
@arr;
#replace the commands above.

I used

splice(@arr)

to replace the command. But I was wondering if there's a difference between writing

@arr = (); 

and

@arr;
Zwettekop
  • 93
  • 7
  • 3
    Why do you think these are the same? In what way did you compare the code? – lurker Jun 25 '19 at 22:07
  • 2
    Your question is unclear. Who says the code is more readable? Why do you assign elements to an array to make it empty in the next line? – choroba Jun 25 '19 at 22:07
  • 4
    Is the code in the question used in a larger statement? If it's assigned to something, this makes a difference -- context matters in Perl. – Grinnz Jun 25 '19 at 22:15

3 Answers3

4
my @arr = ();

is a cluttered, inefficient way of writing

my @arr;

What it does: It creates a new empty lexically-scopped array named @arr.

Lexically-scoped means @arr is only visible (i.e. can only be used) within the inner-most curlies (or file) that contains it.

Assigning an empty list to array empties it, but newly created arrays are guaranteed to be empty already.


my @arr = qw( A B C D );

is equivalent to

my @arr = split(' ', q( A B C D ));

It's a convenient (i.e. shorter) way of writing

my @arr = ( 'A', 'B', 'C', 'D' );

What it does: It creates a new lexically-scopped array named @arr, and assigns four strings (A, B, C and D) to it.

You'd get the same result from

my @arr;
$arr[0] = 'A';
$arr[1] = 'B';
$arr[2] = 'C';
$arr[3] = 'D';

(@arr, @arr = ( )) makes no sense. It amounts to @arr = ().

my @arr = qw (A B C D); my @arr = (); makes no sense. It amounts to my @arr;.

ikegami
  • 367,544
  • 15
  • 269
  • 518
1

my @arr = (); declares a perl variable @arr as local to the scope of the enclosed block. The declaration in the title: @a, @a = () defines @a globally. These links can provide you some help:

How should I use the "my" keyword in Perl?

Why declare Perl variable with "my" at file scope?

and from perldoc:

https://perldoc.perl.org/functions/my.html

if you used modules like use strict, your program would fail if variable were not defined with my.

my @arr = qw(foo bar) is the Perl style. It is more or less convention of the Perl language (e.g. how Python code is Pythonic).

  • 2
    And if you don't use `use strict`, then don't worry, I've already sent over the strike team to set you straight. – Silvio Mayolo Jun 25 '19 at 22:11
  • Please do use `use strict;` while it might kill you with error bombardment, it will make your code behave a little more sanely. :D – oaccamsrazor Jun 27 '19 at 17:02
1

my @arr = declares an array and assigns a list to it.

In your first example, the list is one formed by qw, which "Evaluates to a list of the words extracted out of STRING, using embedded whitespace as the word delimiters." So qw( A B C D ) is the list 'A','B','C','D'. Other delimiters than () can be used, just like with other quote-type operators.

In your second example, you are assigning an empty list to the new array. To make it equivalent to the first example without using qw, it could be:

my @arr = ('A','B','C','D');

or even:

my @arr = 'A'..'D';

qw is there for convenience; most people like it, some do not. Use whatever works for you.

ysth
  • 96,171
  • 6
  • 121
  • 214
  • Sorry I'm new here. I phrased the question wrong, is it better to delete this post and create a new one or should i just leave this one up? – Zwettekop Jun 26 '19 at 22:26
  • @ZwarteKop if you are significantly changing the question, better to delete and create a new one. but I totally don't understand your updated question; can you show more of the original code or context? what are you splicing? what variables do you have to start with and what variables and values for them do you want to end up with? – ysth Jun 26 '19 at 22:45
  • Well i just looked and this question was so bad I can't ask a new one for a couple days. I think i'll wait and then post a new one with more context. Then i can get timed out again for proving to much detail. I'm really sorry for wasting your time guys. Context: Our prof in uni asked us to replicate push, pop and shift with only slice. Then he asked us to replicate the 2 cmd's above. You can also do this with splice because if you only give an array it just deletes it. I think. The question is what's up with the weird notation to delete the array? I really hope this helps. I'm sorry guys. – Zwettekop Jun 26 '19 at 22:50
  • which two commands? it wasn't clear. `@arr = ();` is `splice(@arr)`, but what is the other one? when you include the `my` you are confusing things, since declaring an array is not something splice can do – ysth Jun 26 '19 at 22:55
  • replicate @arr; and @arr() with splice. The solution is splice(@arr) because the perldoc says "If both OFFSET and LENGTH are omitted, (splice) removes everything." – Zwettekop Jun 26 '19 at 23:08