Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default array push

    How come when I push a variable to an array it puts one whitespace before
    the variables on all the lines except the first one? I would except all
    the lines not to have the extra white space.

    Is there an easier way to fill an array with a variable without using the
    push?

    $last printed out
    TMR2,mpd_gw,50,w32-ix86,client
    TMR2,mpd_gw,50,w32-ix86,client
    TMR2,mpd_gw,50,w32-ix86,client
    TMR2,mpd_gw,50,w32-ix86,client


    @temparray printed out
    TMR2,mpd_gw,50,w32-ix86,client
    TMR2,mpd_gw,50,w32-ix86,client
    TMR2,mpd_gw,50,w32-ix86,client
    TMR2,mpd_gw,50,w32-ix86,client



    Code:
    if (something){
    $last = "$tmrname,$gateway_hash{$gateway},$version,$interp ,$type\n";
    #print "$last";
    push(@temparray, $last);
    }

    @temparray = sort @temparray;
    print "@temparray";



    Anthony J Segelhorst
    Enterprise Systems Management Team
    Phone: 937-495-1876
    Email: [email]ajs15@meadwestvaco.com[/email]

    __________________________________________________ ______________________
    This email has been scanned for all viruses by the MessageLabs SkyScan
    service.__________________________________________ _____________________

    Note: Please update your email address for this user to reflect the
    new MeadWestvaco Corporation. MeadWestvaco employee email addresses
    are in the format of [email]user@MeadWestvaco.com[/email]

    This electronic message contains information from MeadWestvaco
    Corporation or subsidiary companies, which may be confidential,
    privileged or otherwise protected from disclosure. The
    information is intended to be used solely by the recipient(s)
    named. If you are not an intended recipient, be aware that
    any review, disclosure, copying, distribution or use of this
    transmission or its contents is prohibited. If you have
    received this transmission in error, please notify MeadWestvaco
    immediately at [email]postmaster@MeadWestvaco.com[/email].
    __________________________________________________ _____________________
    Anthony J Segelhorst Guest

  2. Similar Questions and Discussions

    1. Push XML to FMS
      Can FMS create an XML socket that listens for and accepts connections from external services? I know that FMS can create an XML socket and...
    2. Push URL
      I am trying to create a URL push call to send a url that will open a browser and navigate to the url for all users in the room. Does anyone have any...
    3. Need a bit of a push...
      OK, ive been given a brief to simulate a tank. Ie, a flexible track around several wheels that drives a tank across a terrain. Anyone know of...
    4. push
      hey all, i know a little about flash, but my problem is that, i want to make a counter-strike cartoon, but I want to press a little button in the...
    5. [PHP] Array Push and Keys
      On Thu, 28 Aug 2003 18:14:39 -0400, you wrote: Ahem. Count the brackets? :) (On a style point, your code would be easier to read for bugs...
  3. #2

    Default Re: array push

    At 11:17 AM 1/26/04 -0500, you wrote:
    >print "@temparray";
    Take the quotes off.

    Tim Guest

  4. #3

    Default Re: array push

    For Quality purpouses, Anthony J Segelhorst 's mail on Monday 26 January 2004
    17:17 may have been monitored or recorded as:
    > How come when I push a variable to an array it puts one whitespace before
    > the variables on all the lines except the first one? *I would except all
    > the lines not to have the extra white space.
    ..
    > print "@temparray";
    Try print @temparay;

    since you have a "\n" at the end of your array (btw: do you really want
    that?) , your records are seperated by something when you print them.
    print " " puts a space in between the eleent as a convienience for people who
    dont have a "\n" at the end of their emelents.

    hth, wolf

    Wolf Blaum Guest

  5. #4

    Default Re: array push

    >For Quality purposes, Anthony J Segelhorst 's mail on Monday 26 January
    2004
    >17:17 may have been monitored or recorded as:
    > How come when I push a variable to an array it puts one whitespace
    before
    > the variables on all the lines except the first one? *I would except all
    > the lines not to have the extra white space.
    ...
    > print "@temparray";
    >Try print @temparay;
    >since you have a "\n" at the end of your array (btw: do you really want
    >that?) , your records are seperated by something when you print them.
    >print " " puts a space in between the eleent as a convienience for people
    who
    >dont have a "\n" at the end of their emelents.
    print @temparay;


    This worked, thanks.



    Anthony J Segelhorst
    Enterprise Systems Management Team
    Phone: 937-495-1876
    Email: [email]ajs15@meadwestvaco.com[/email]





    wolf blaum <blaum@uthscsa.edu>
    01/26/2004 11:39 AM


    To: 
    cc:
    Subject: Re: array push


    For Quality purpouses, Anthony J Segelhorst 's mail on Monday 26 January
    2004
    17:17 may have been monitored or recorded as:
    > How come when I push a variable to an array it puts one whitespace
    before
    > the variables on all the lines except the first one? *I would except all
    > the lines not to have the extra white space.
    ...
    > print "@temparray";
    Try print @temparay;

    since you have a "\n" at the end of your array (btw: do you really want
    that?) , your records are seperated by something when you print them.
    print " " puts a space in between the eleent as a convienience for people
    who
    dont have a "\n" at the end of their emelents.

    hth, wolf


    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>



    __________________________________________________ ______________________
    This email has been scanned for all viruses by the MessageLabs SkyScan
    service.__________________________________________ _____________________



    __________________________________________________ ______________________
    This email has been scanned for all viruses by the MessageLabs SkyScan
    service.__________________________________________ _____________________

    Note: Please update your email address for this user to reflect the
    new MeadWestvaco Corporation. MeadWestvaco employee email addresses
    are in the format of [email]user@MeadWestvaco.com[/email]

    This electronic message contains information from MeadWestvaco
    Corporation or subsidiary companies, which may be confidential,
    privileged or otherwise protected from disclosure. The
    information is intended to be used solely by the recipient(s)
    named. If you are not an intended recipient, be aware that
    any review, disclosure, copying, distribution or use of this
    transmission or its contents is prohibited. If you have
    received this transmission in error, please notify MeadWestvaco
    immediately at [email]postmaster@MeadWestvaco.com[/email].
    __________________________________________________ _____________________
    Anthony J Segelhorst Guest

  6. #5

    Default Re: array push

    Wolf Blaum wrote:
    >
    > For Quality purpouses, Anthony J Segelhorst 's mail on Monday 26 January 2004
    > 17:17 may have been monitored or recorded as:
    >
    > > How come when I push a variable to an array it puts one whitespace before
    > > the variables on all the lines except the first one? I would except all
    > > the lines not to have the extra white space.
    > ..
    > > print "@temparray";
    >
    > Try print @temparay;
    >
    > since you have a "\n" at the end of your array (btw: do you really want
    > that?) , your records are seperated by something when you print them.
    > print " " puts a space in between the eleent as a convienience for people who
    > dont have a "\n" at the end of their emelents.
    The right conclusion for the wrong reasons Wolf! The spaces are the result of
    interpolating the array into a string, and the presence of a newline on each
    array element is immaterial:

    my @arr = ('A', 'B', 'C', 'D');
    print "@arr\n";
    print @arr, "\n";

    **OUTPUT

    A B C D
    ABCD

    In general, it's a bad idea to put variables inside double quotes unless know
    what it means.

    my $var = $value

    is much more likely to be right than

    my $var = "$value"

    and the latter is very similar to adding zero to a numeric varibale.

    Cheers,

    Rob



    Rob Dixon Guest

  7. #6

    Default Re: array push

    For Quality purpouses, Rob Dixon 's mail on Tuesday 27 January 2004 00:30may
    have been monitored or recorded as:
    > The right conclusion for the wrong reasons Wolf! The spaces are the result
    > of interpolating the array into a string, and the presence of a newline on
    > each array element is immaterial:
    >
    > my @arr = ('A', 'B', 'C', 'D');
    > print "@arr\n";
    > print @arr, "\n";
    >
    > **OUTPUT
    >
    > A B C D
    > ABCD
    Hi Rob,

    well, maybe Im totally wrong here, but getting these results :

    $last printed out
    TMR2,mpd_gw,50,w32-ix86,client
    TMR2,mpd_gw,50,w32-ix86,client
    TMR2,mpd_gw,50,w32-ix86,client
    TMR2,mpd_gw,50,w32-ix86,client


    @temparray printed out
    TMR2,mpd_gw,50,w32-ix86,client
    *TMR2,mpd_gw,50,w32-ix86,client
    *TMR2,mpd_gw,50,w32-ix86,client
    *TMR2,mpd_gw,50,w32-ix86,client
    *

    from this code:

    Code:
    if (something){
    $last = "$tmrname,$gateway_hash{$gateway},$version,$interp ,$type\n";
    * * * * #print "$last";
    * * * * push(@temparray, $last);
    }

    @temparray = sort @temparray;
    print "@temparray";

    Anthony asked, where the spaces came from, expecting the output of $last and
    @temparray to be the same.
    Of course, you are right about the interpolation used before the print : i
    just meant to point out that the newlines AND the spaces in his print
    "@temparray" are a result of the quotes used with print, and his attachment
    of \n to $last before the push @temparray,$last - without making it to
    complicated.
    (Tim also hinted to $").

    Or did I totaly miss something.

    thx, wolf









    Wolf Blaum Guest

  8. #7

    Default Re: array push

    Wolf Blaum wrote:
    >
    > For Quality purpouses, Rob Dixon 's mail on Tuesday 27 January 2004 00:30 may
    > have been monitored or recorded as:
    >
    > > The right conclusion for the wrong reasons Wolf! The spaces are the result
    > > of interpolating the array into a string, and the presence of a newline on
    > > each array element is immaterial:
    > >
    > > my @arr = ('A', 'B', 'C', 'D');
    > > print "@arr\n";
    > > print @arr, "\n";
    > >
    > > **OUTPUT
    > >
    > > A B C D
    > > ABCD
    >
    > well, maybe Im totally wrong here, but getting these results :
    >
    > $last printed out
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    Yes. These are the values of each string before they are pushed onto the array
    (In the order they are pushed but before they're sorted).
    > @temparray printed out
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    Didn't you mean to put spaces before the last three records? This will be the
    result of

    print @temparray;

    which is the same as

    print $temparray[0], $temparray[1], $temparray[2], $temparray[3];

    not

    print "@temparray";

    which is the same as

    print join ' ', @temparray;
    > from this code:
    >
    > Code:
    > if (something){
    > $last = "$tmrname,$gateway_hash{$gateway},$version,$interp ,$type\n";
    > #print "$last";
    > push(@temparray, $last);
    > }
    >
    > @temparray = sort @temparray;
    > print "@temparray";
    >
    > Anthony asked, where the spaces came from, expecting the output of $last and
    > @temparray to be the same.
    >
    > Of course, you are right about the interpolation used before the print : i
    > just meant to point out that the newlines AND the spaces in his print
    > "@temparray" are a result of the quotes used with print, and his attachment
    > of \n to $last before the push @temparray,$last - without making it to
    > complicated.
    Thew newlines in the output are there because they were in the original
    strings. The spaces after the newlines are added because that's how arrays
    are interpolated in double-quote context, but they're nothing to do with the
    original newline! Whatever the last character in each element is, the interpolation
    "@temparray" puts a space between the elements, as I showed with my simpler example
    with just single-letter strings.
    > (Tim also hinted to $").
    Yes. Strictly "@temparray" is the same as

    join $", @temparray

    but $" is one of the built-in variables that I think it's best to forget about,
    as if you want something other than the default you can just write a 'join'
    with the separator you want and avoid obscure code.
    > Or did I totaly miss something.
    Maybe. I'm not sure where you're misunderstanding what I wrote. Anyway, I hope this
    helps.

    Rob

    Rob Dixon Guest

  9. #8

    Default Re: array push

    For Quality purpouses, Rob Dixon 's mail on Tuesday 27 January 2004 12:42 may
    have been monitored or recorded as:
    > Didn't you mean to put spaces before the last three records? This will be
    > the result of
    >
    ...
    well - in Anthonys original mail there were spaces - i just copied that.
    > > Or did I totaly miss something.
    >
    > Maybe. I'm not sure where you're misunderstanding what I wrote. Anyway, I
    > hope this helps.
    ok - I think we are talking about the same thing here.
    thx, wolf

    Wolf Blaum Guest

  10. #9

    Default Re: array push

    Wolf Blaum wrote:
    >
    > For Quality purpouses, Rob Dixon 's mail on Tuesday 27 January 2004 00:30 may
    > have been monitored or recorded as:
    >
    > > The right conclusion for the wrong reasons Wolf! The spaces are the result
    > > of interpolating the array into a string, and the presence of a newline on
    > > each array element is immaterial:
    > >
    > > my @arr = ('A', 'B', 'C', 'D');
    > > print "@arr\n";
    > > print @arr, "\n";
    > >
    > > **OUTPUT
    > >
    > > A B C D
    > > ABCD
    >
    > well, maybe Im totally wrong here, but getting these results :
    >
    > $last printed out
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    Yes. These are the values of each string before they are pushed onto the array
    (In the order they are pushed but before they're sorted).
    > @temparray printed out
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    > TMR2,mpd_gw,50,w32-ix86,client
    Didn't you mean to put spaces before the last three records? This will be the
    result of

    print @temparray;

    which is the same as

    print $temparray[0], $temparray[1], $temparray[2], $temparray[3];

    not

    print "@temparray";

    which is the same as

    print join ' ', @temparray;
    > from this code:
    >
    > Code:
    > if (something){
    > $last = "$tmrname,$gateway_hash{$gateway},$version,$interp ,$type\n";
    > #print "$last";
    > push(@temparray, $last);
    > }
    >
    > @temparray = sort @temparray;
    > print "@temparray";
    >
    > Anthony asked, where the spaces came from, expecting the output of $last and
    > @temparray to be the same.
    >
    > Of course, you are right about the interpolation used before the print : i
    > just meant to point out that the newlines AND the spaces in his print
    > "@temparray" are a result of the quotes used with print, and his attachment
    > of \n to $last before the push @temparray,$last - without making it to
    > complicated.
    Thew newlines in the output are there because they were in the original
    strings. The spaces after the newlines are added because that's how arrays
    are interpolated in double-quote context, but they're nothing to do with the
    original newline! Whatever the last character in each element is, the interpolation
    "@temparray" puts a space between the elements, as I showed with my simpler example
    with just single-letter strings.
    > (Tim also hinted to $").
    Yes. Strictly "@temparray" is the same as

    join $", @temparray

    but $" is one of the built-in variables that I think it's best to forget about,
    as if you want something other than the default you can just write a 'join'
    with the separator you want and avoid obscure code.
    > Or did I totaly miss something.
    Maybe. I'm not sure where you're misunderstanding what I wrote. Anyway, I hope this
    helps.

    Rob


    Rob Dixon Guest

  11. #10

    Default Re: array push

    Wolf Blaum wrote:
    >
    > For Quality purpouses, Rob Dixon 's mail on Tuesday 27 January 2004 12:42 may
    > have been monitored or recorded as:
    >
    > > Didn't you mean to put spaces before the last three records? This will be
    > > the result of
    > >
    > ..
    > well - in Anthonys original mail there were spaces - i just copied that.
    But my point was that your post /didn't/ include the spaces :)
    > > > Or did I totaly miss something.
    > >
    > > Maybe. I'm not sure where you're misunderstanding what I wrote. Anyway, I
    > > hope this helps.
    >
    > ok - I think we are talking about the same thing here.
    I hope so, but I'm still a little worried that you think it's to do
    with having newline characters at the ends of the array elements. It
    isn't!

    Rob


    Rob Dixon Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139