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

Array qw difference

by dnamonk (Acolyte)
on Nov 29, 2021 at 23:24 UTC ( [id://11139249]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

I am just trying to understand the array mechanism in PERL. Given the below code can you tell me why the same array isn't working in qw same like the first one above?

#!/bin/bash/perl @tests = ("ABCGE ABCGE", "ABCGE FGCGB", "ABCGE JHAGT"); my @tests = ( [qw{ABCGE ABCGE}], [qw{ABCGE FGCGB}], [qw{ABCGE JHAGT}], ); foreach $strings (@tests) { my $diff = 0; for my $i (0 .. length($strings->[0]) - 1) { my @chars = map substr($strings->[$_], $i, 1), 0, 1; ++$diff if $chars[0] ne $chars[1]; } print "@$strings $diff\n"; }
Thank you in advance for help.

Replies are listed 'Best First'.
Re: Array qw difference
by AnomalousMonk (Archbishop) on Nov 30, 2021 at 00:26 UTC
    ... the same array isn't working in qw same like the first one above?

    They're not the same. "ABCGE ABCGE" produces a single string consisting in two alpha sequences separated by a space. qw produces groups (update: i.e., a list of zero or more) of non-space characters.

    Win8 Strawberry 5.8.9.5 (32) Mon 11/29/2021 17:10:24 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dumper; my @t1 = ('ABCGE ABCGE', 'ABCGE FGCGB', 'ABCGE JHAGT'); my @t2 = ( [qw{ABCGE ABCGE}], [qw{ABCGE FGCGB}], [qw{ABCGE JHAGT}], ); print Dumper \@t1; print Dumper \@t2; ^Z $VAR1 = [ 'ABCGE ABCGE', 'ABCGE FGCGB', 'ABCGE JHAGT' ]; $VAR1 = [ [ 'ABCGE', 'ABCGE' ], [ 'ABCGE', 'FGCGB' ], [ 'ABCGE', 'JHAGT' ] ];
    Data::Dumper is core, so you should be able to see this for yourself.


    Give a man a fish:  <%-{-{-{-<

Re: Array qw difference
by hippo (Bishop) on Nov 29, 2021 at 23:36 UTC
      Sorry, I forgot to use #comments for the first array @tests. Also strict and warning. I am just trying to understand why the array assignment is behaving differently between the two. Ah okay you mean the 2nd one is an array of array. Okay, I understand now. It's an array within an array. Got it! Thanks

        The AoA, HoH, etc., nomenclature is discussed at length in the Perl Data Structures Cookbook (perldsc).

        Update: BTW: You've once again updated a post without citation. Naughty, naughty!


        Give a man a fish:  <%-{-{-{-<

Re: Array qw difference
by davido (Cardinal) on Nov 30, 2021 at 16:21 UTC

    The qw// operator is discussed in perlop. The semantics are essentially that within a qw quote-like operator, whitespace separates elements. So: qw{ABCGE ABCGE} represents two elements, not one. This is different behavior from q// or qq//, which would return a single element:

    my @list = qw{ABCDE DEFGH}; # @list has two elements. my @list = qq{ABCDE DEFGH}; # @list has one element. my @list = q{ABCDE DEFGH}; # @list has one element.

    Therefore, these two things ARE equivalent:

    my @list = qw{ABCDE DEFGH}; # Two elements. my @list = ('ABCDE', 'DEFGH'); # Two elements.

    And these are equivalent:

    my @list = ([qw{ABCDE DEFGH}], [qw{IJKLM NOPQR}]); # Two array-refs w +ith two elements in each. my @list = (['ABCDE, 'DEFGH'], ['IJKLM', 'NOPQR']); # Two array-refs w +ith two elements in each.

    And the following produces four elements:

    my @list = ('ABCDE', qw{DEFGH IJKLM}, 'NOPQR');  # Four elements.

    Dave

Re: Array qw difference
by Anonymous Monk on Nov 30, 2021 at 16:28 UTC

    I think you meant to use qq, not qw.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-29 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found