printing array problem

Discussion in 'Perl Misc' started by Mark Day, Sep 28, 2004.

  1. Mark Day

    Mark Day Guest

    Any tips on what I'm doing wrong here appreciated.

    I'm using perl version 5.6.1 on Win98,

    and this test script i've written, I was hoping would print each element
    of this array, on a new line, then print the array again in reverse order.

    What actually output's is the same array twice with all elements printed
    on the same line.

    What is wrong with my attempt to reverse the array, adn why does each
    scalar not print on a new line?

    thanks.

    --code snippet---

    #!C:\perl\bin\perl

    @array = qq(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);

    foreach (@array)

    {

    print $_;

    print "\n";

    }

    @array = reverse @array;

    foreach (@array) {

    print $_;

    print "\n";

    }
    Mark Day, Sep 28, 2004
    #1
    1. Advertising

  2. Mark Day wrote:
    > Any tips on what I'm doing wrong here appreciated.
    >
    > I'm using perl version 5.6.1 on Win98,
    >
    > and this test script i've written, I was hoping would print each element
    > of this array, on a new line, then print the array again in reverse order.
    >
    > What actually output's is the same array twice with all elements printed
    > on the same line.
    >
    > What is wrong with my attempt to reverse the array, adn why does each
    > scalar not print on a new line?
    >
    > thanks.
    >
    > --code snippet---
    >
    > #!C:\perl\bin\perl
    >
    > @array = qq(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);


    qq() gives you an interpolated string - man perlop. what you are
    effectively doing here is

    @array = "1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen";

    which will set $array[0] to "1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen"
    and leave the rest of the array empty.

    you probably mean

    my @array = qw(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);

    though that will give you "baker's" and "dozen" as separate array elements.

    You can also skip the

    @array = reverse @array

    step

    ie

    foreach( reverse @array){

    ....

    Mark
    Mark Clements, Sep 28, 2004
    #2
    1. Advertising

  3. Mark Day

    Mark Day Guest

    Mark Clements wrote:

    > qq() gives you an interpolated string - man perlop. what you are
    > effectively doing here is
    >
    > @array = "1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen";
    >
    > which will set $array[0] to "1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen"
    > and leave the rest of the array empty.
    >
    > you probably mean
    >
    > my @array = qw(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);
    > though that will give you "baker's" and "dozen" as separate array elements.
    >
    > You can also skip the
    >
    > @array = reverse @array
    >
    > step
    >
    > ie
    >
    > foreach( reverse @array){
    >
    > ...
    >
    > Mark



    Great! thanks.
    Mark Day, Sep 28, 2004
    #3
  4. Mark Day

    Anno Siegel Guest

    Mark Day <> wrote in comp.lang.perl.misc:
    > Any tips on what I'm doing wrong here appreciated.
    >
    > I'm using perl version 5.6.1 on Win98,
    >
    > and this test script i've written, I was hoping would print each element
    > of this array, on a new line, then print the array again in reverse order.
    >
    > What actually output's is the same array twice with all elements printed
    > on the same line.
    >
    > What is wrong with my attempt to reverse the array, adn why does each
    > scalar not print on a new line?
    >
    > thanks.
    >
    > --code snippet---
    >
    > #!C:\perl\bin\perl
    >
    > @array = qq(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);


    [snip]

    Look up what qq() does. I guess you meant to say qw() instead.

    Anno
    Anno Siegel, Sep 28, 2004
    #4
  5. Mark Day

    Graham Wood Guest

    Mark Day wrote:
    >
    > hoping would print each element
    > of this array, on a new line, then print the array again in reverse order.
    >
    > What is wrong with my attempt to reverse the array, adn why does each
    > scalar not print on a new line?
    >
    >
    > #!C:\perl\bin\perl
    >
    > @array = qq(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);


    Your problem is that you're using qq which will surround the contents of
    the parenthesis with double quotes rather than qw which will surround
    each word in the parenthesis with double quotes. You are basically
    creating and array with one element in it: "1 2 3 4 5 6 7 8 9 10 11 12
    baker's dozen" and reversing this then gives you the same single element
    in reverse order.

    Replace qq with qw and see what happens.

    Graham

    >
    > foreach (@array)
    >
    > {
    >
    > print $_;
    >
    > print "\n";
    >
    > }
    >
    > @array = reverse @array;
    >
    > foreach (@array) {
    >
    > print $_;
    >
    > print "\n";
    >
    > }
    Graham Wood, Sep 28, 2004
    #5
  6. Mark Day

    Anno Siegel Guest

    Graham Wood <> wrote in comp.lang.perl.misc:
    > Mark Day wrote:


    [...]

    > > @array = qq(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);

    >
    > Your problem is that you're using qq which will surround the contents of
    > the parenthesis with double quotes rather than qw which will surround
    > each word in the parenthesis with double quotes.


    That is wrong. "qw()" surrounds the whole string with single quotes,
    then splits the result on whitespace.

    Anno
    Anno Siegel, Sep 28, 2004
    #6
  7. Mark Day

    Anno Siegel Guest

    Anno Siegel <-berlin.de> wrote in comp.lang.perl.misc:
    > Graham Wood <> wrote in comp.lang.perl.misc:
    > > Mark Day wrote:

    >
    > [...]
    >
    > > > @array = qq(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);

    > >
    > > Your problem is that you're using qq which will surround the contents of
    > > the parenthesis with double quotes rather than qw which will surround
    > > each word in the parenthesis with double quotes.

    >
    > That is wrong. "qw()" interprets the whole string in single-quote
    > style, then splits the result on whitespace.
    >
    > Anno
    Anno Siegel, Sep 28, 2004
    #7
  8. Mark Day

    Anno Siegel Guest

    Graham Wood <> wrote in comp.lang.perl.misc:
    > Mark Day wrote:


    [...]

    > > @array = qq(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);

    >
    > Your problem is that you're using qq which will surround the contents of
    > the parenthesis with double quotes rather than qw which will surround
    > each word in the parenthesis with double quotes.


    That is wrong. "qw()" treats the whole string as single quoted,
    then splits the result on whitespace.

    Anno
    Anno Siegel, Sep 28, 2004
    #8
  9. Mark Day

    Graham Wood Guest

    Anno Siegel wrote:
    >
    > Graham Wood <> wrote in comp.lang.perl.misc:
    > > Mark Day wrote:

    >
    > [...]
    >
    > > > @array = qq(1 2 3 4 5 6 7 8 9 10 11 12 baker's dozen);

    > >
    > > Your problem is that you're using qq which will surround the contents of
    > > the parenthesis with double quotes rather than qw which will surround
    > > each word in the parenthesis with double quotes.

    >
    > That is wrong. "qw()" treats the whole string as single quoted,
    > then splits the result on whitespace.



    Oh. Thanks

    Graham

    >
    > Anno
    Graham Wood, Sep 28, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon B
    Replies:
    7
    Views:
    7,500
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. JustSomeGuy
    Replies:
    13
    Views:
    506
    msalters
    Dec 9, 2004
  3. iffy agbim
    Replies:
    1
    Views:
    105
    Mark Andrews
    May 24, 2004
  4. iffy agbim
    Replies:
    0
    Views:
    101
    iffy agbim
    May 21, 2004
Loading...

Share This Page