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

  1. #1

    Default Re: copy list until...

    On 17 Sep 2003 [email]jimbo@jimbo.com[/email] wrote:
    >I would like to copy the list of presidents up to but not including
    >current or future presidents (not for any particular reason):
    >
    >my @presidents = ( 'Roosevelt' , 'Kennedy' , 'Reagan' , 'Clinton' , 'Bush' , 'Jenna Jameson' );
    >my @oldpresidents;
    @oldpresidents = grep { "true" .. $_ eq "Bush" } @presidents;

    See 'perlop' for the .. operator (flip-flop, not range).

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

    Jeff 'japhy' Pinyan Guest

  2. Similar Questions and Discussions

    1. copy a part of a list to another list
      i store my search result in a list and want to divide that list into 3 or 4 smaller lists How can i do this please help..
    2. ibm flash copy, hp business copy
      dfreybur@yahoo.com (Doug Freyburger) wrote in message news:<7960d3ee.0406080727.42c9875a@posting.google.com>... but in either case, we still...
    3. previous favorites list copy comment
      Regarding the comment earlier about copying one's favorites list, thank you for that info I've always wanted. Does someone know how to proceed...
    4. cdrecord copy destroyed another windows copy !!!
      # cdrecord -msinfo dev=1,1,0 RAW/R16 0,221691 # cdrecord -msinfo dev=1,1,0 RAW/R16 44317,51858 what can be implied by those 2 messages ? How...
    5. cdrecord copy destroyed another windows NERO copy for re-writable media
      # cdrecord -msinfo dev=1,1,0 RAW/R16 0,221691 # cdrecord -msinfo dev=1,1,0 RAW/R16 44317,51858 what can be implied by those 2 messages ? How...
  3. #2

    Default Re: copy list until...

    Using the $_ 'default' will help clean up your code a little, like so :-

    my @presidents = qw(Roosevelt Kennedy Reagan Clinton Bush Jenna_Jameson );
    my @oldpresidents;

    foreach (@presidents)
    {
    last if( /^Bush$/ );
    push @oldpresidents, $_;
    }

    Dagmar




    <jimbo@jimbo.com> wrote in message
    news:3f6860f0$0$58715$e4fe514c@news.xs4all.nl...
    > Hi,
    >
    > I would like to copy the list of presidents up to but not including
    current or
    > future presidents (not for any particular reason):
    >
    > my @presidents = ( 'Roosevelt' , 'Kennedy' , 'Reagan' , 'Clinton' , 'Bush'
    , 'Jenna Jameson' );
    > my @oldpresidents;
    >
    > foreach my $president ( @presidents )
    > {
    > last if( $president eq 'Bush' );
    > push( @oldpresidents , $president );
    > }
    >
    > Is there a more perlquick way to do this?
    >

    ---
    This e-mail has been virus scanned and is certified virus Free.
    Checked by AVG anti-virus system ([url]http://www.grisoft.com[/url]).
    Version: 6.0.518 / Virus Database: 316 - Release Date: 9/11/03


    D Borland Guest

  4. #3

    Default Re: copy list until...

    On Wed, 17 Sep 2003 09:55:26 -0400, Jeff 'japhy' Pinyan wrote:
    >>my @presidents = ( 'Roosevelt' , 'Kennedy' , 'Reagan' , 'Clinton' , 'Bush' , 'Jenna Jameson' );
    >>my @oldpresidents;
    >
    > @oldpresidents = grep { "true" .. $_ eq "Bush" } @presidents
    I was trying to understand how this worked but it seems it does not :-)

    Argument "true" isn't numeric in range (or flip) at try line 5.
    Use of uninitialized value in range (or flip) at try line 5.
    Use of uninitialized value in range (or flip) at try line 5.
    Roosevelt
    Kennedy
    Reagan
    Clinton
    Bush
    Jenna Jameson

    (Perl 5.8.0 OS/2)

    Regards

    Dave Saville

    NB switch saville for nospam in address


    Dave Saville Guest

  5. #4

    Default Re: copy list until...

    [email]jimbo@jimbo.com[/email] (jimbo@jimbo.com) wrote on MMMDCLXIX September MCMXCIII
    in <URL:news:3f6860f0$0$58715$e4fe514c@news.xs4all.nl >:
    "" Hi,
    ""
    "" I would like to copy the list of presidents up to but not including current
    "" future presidents (not for any particular reason):
    ""
    "" my @presidents = ( 'Roosevelt' , 'Kennedy' , 'Reagan' , 'Clinton' , 'Bush' , 'Jenna Jameson' );

    So, what happened with Truman, Eisenhower, Johson, Nixon, Ford and Carter?
    And how are you going to distinguish between papa Bush and baby Bush,
    if all you have are last names?

    "" my @oldpresidents;
    ""
    "" foreach my $president ( @presidents )
    "" {
    "" last if( $president eq 'Bush' );
    "" push( @oldpresidents , $president );
    "" }
    ""
    "" Is there a more perlquick way to do this?
    ""

    #!/usr/bin/perl

    use strict;
    use warnings;

    my @pres = (qw /Roosevelt Truman Eisenhouwer Kenndey Johnson Nixon Ford
    Carter Reagan/, "papa Bush", "Clinton", "baby Bush",
    "Jenna Jameson");

    my $f = 0;
    my @old = grep {! (/baby Bush/ .. $f)} @pres;

    print "@old\n";
    __END__
    Roosevelt Truman Eisenhouwer Kenndey Johnson Nixon Ford Carter Reagan
    papa Bush Clinton

    --
    # Count the number of lines; code doesn't match \w. Linux specific.
    ()=<>;$!=$=;($:,$,,$;,$")=$!=~/.(.)..(.)(.)..(.)/;
    $;++;$*++;$;++;$*++;$;++;`$:$,$;$" $. >&$*`;
    Abigail Guest

  6. #5

    Default Re: copy list until...

    [email]jimbo@jimbo.com[/email] <jimbo@jimbo.com> wrote:
    > up to but not including current or
    > my @presidents = ( 'Roosevelt' , 'Kennedy' , 'Reagan' , 'Clinton' , 'Bush'
    > last if( $president eq 'Bush' );

    In order to exclude the current Bush and include the previous Bush,
    you would need some way of distinguishing between the two.

    <grin>


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  7. #6

    Default Re: copy list until...

    On Wed, 17 Sep 2003, Dave Saville wrote:
    >On Wed, 17 Sep 2003 09:55:26 -0400, Jeff 'japhy' Pinyan wrote:
    >
    >>>my @presidents = ( 'Roosevelt' , 'Kennedy' , 'Reagan' , 'Clinton' , 'Bush' , 'Jenna Jameson' );
    >>>my @oldpresidents;
    >>
    >> @oldpresidents = grep { "true" .. $_ eq "Bush" } @presidents
    >
    >I was trying to understand how this worked but it seems it does not :-)
    Wow, I did so many wrong things. I thought the operand had to be numeric
    for Perl to implicitly compare it to $., and I expected scalar context IN
    the grep (shouldn't it be?), and I thought the flip-flop operator would
    work in this case. It doesn't seem to work easily.

    Sorry I didn't test that code...

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

    Jeff 'japhy' Pinyan Guest

  8. #7

    Default Re: copy list until...

    >>>>> "J'P" == Jeff 'japhy' Pinyan <pinyaj@rpi.edu> writes:
    >>> @oldpresidents = grep { "true" .. $_ eq "Bush" } @presidents
    >>
    >> I was trying to understand how this worked but it seems it does not :-)
    J'P> Wow, I did so many wrong things. I thought the operand had to be numeric
    J'P> for Perl to implicitly compare it to $., and I expected scalar context IN
    J'P> the grep (shouldn't it be?), and I thought the flip-flop operator would
    J'P> work in this case. It doesn't seem to work easily.

    grep EXPR is done in scalar context but the block provides list context
    it seems. i don't think that is comparing anything to $. the problem is
    ... in list context is trying to make a range of vals from 'true' to
    undef or whatever eq returns as false. so list .. barfs since one of the args
    is a string and the other is a number.

    and with a little fooling around i can't seem to get the .. to work
    either. i think it seems to be freshly evaluated when entering the block
    so it gets reset. not sure. but i have used scalar .. many times in
    plain conditionals.

    uri

    --
    Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
    Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
    Uri Guttman Guest

  9. #8

    Default Re: copy list until...


    <jimbo@jimbo.com> wrote in message
    news:3f6860f0$0$58715$e4fe514c@news.xs4all.nl...
    > Hi,
    >
    > I would like to copy the list of presidents up to but not including
    current or
    > future presidents (not for any particular reason):
    >
    > my @presidents = ( 'Roosevelt' , 'Kennedy' , 'Reagan' , 'Clinton' , 'Bush'
    , 'Jenna Jameson' );

    Last I checked, there is a 'Bush' on both sides on 'Clinton'


    --
    brian


    Brian Helterline Guest

  10. #9

    Default Re: copy list until...

    Brian Helterline wrote:
    >
    > Last I checked, there is a 'Bush' on both sides on 'Clinton'
    >
    Isn't there always?

    Chris Mattern

    Chris Mattern Guest

  11. #10

    Default Re: copy list until...

    On Wed, 17 Sep 2003 16:54:27 -0400, Chris Mattern <syscjm@gwu.edu>
    wrote:
    >Brian Helterline wrote:
    >>
    >> Last I checked, there is a 'Bush' on both sides on 'Clinton'
    >>
    >Isn't there always?
    He he.
    --
    Helgi Briem hbriem AT simnet DOT is

    Excuse the munged address. My last
    e-mail address was killed by spammers.
    Helgi Briem Guest

  12. #11

    Default Re: copy list until...

    Uri Guttman <uri@stemsystems.com> wrote in comp.lang.perl.misc:
    > >>>>> "J'P" == Jeff 'japhy' Pinyan <pinyaj@rpi.edu> writes:
    >
    > >>> @oldpresidents = grep { "true" .. $_ eq "Bush" } @presidents
    > >>
    > >> I was trying to understand how this worked but it seems it does not :-)
    >
    > J'P> Wow, I did so many wrong things. I thought the operand had to be numeric
    > J'P> for Perl to implicitly compare it to $., and I expected scalar context IN
    > J'P> the grep (shouldn't it be?), and I thought the flip-flop operator would
    > J'P> work in this case. It doesn't seem to work easily.
    >
    > grep EXPR is done in scalar context but the block provides list context
    > it seems. i don't think that is comparing anything to $. the problem is
    > .. in list context is trying to make a range of vals from 'true' to
    > undef or whatever eq returns as false. so list .. barfs since one of the args
    > is a string and the other is a number.
    >
    > and with a little fooling around i can't seem to get the .. to work
    > either. i think it seems to be freshly evaluated when entering the block
    > so it gets reset. not sure. but i have used scalar .. many times in
    > plain conditionals.
    ".." does an implicit compare with $. when it sees a constant. (It
    would be nicer if it only did that for numeric constants.) The solution
    is to use a non-constant expression. "do { 1}" and "do { 0}" work
    nicely for true and false. So:

    my @oldpresidents = grep { do { 1} .. $_ eq 'Bush' } @presidents;

    Anno
    Anno Siegel Guest

  13. #12

    Default Re: copy list until...

    Uri Guttman <uri@stemsystems.com> wrote in comp.lang.perl.misc:
    > >>>>> "J'P" == Jeff 'japhy' Pinyan <pinyaj@rpi.edu> writes:
    >
    > >>> @oldpresidents = grep { "true" .. $_ eq "Bush" } @presidents
    > >>
    > >> I was trying to understand how this worked but it seems it does not :-)
    >
    > J'P> Wow, I did so many wrong things. I thought the operand had to be numeric
    > J'P> for Perl to implicitly compare it to $., and I expected scalar context IN
    > J'P> the grep (shouldn't it be?), and I thought the flip-flop operator would
    > J'P> work in this case. It doesn't seem to work easily.
    >
    > grep EXPR is done in scalar context but the block provides list context
    > it seems. i don't think that is comparing anything to $. the problem is
    > .. in list context is trying to make a range of vals from 'true' to
    > undef or whatever eq returns as false. so list .. barfs since one of the args
    > is a string and the other is a number.
    >
    > and with a little fooling around i can't seem to get the .. to work
    > either. i think it seems to be freshly evaluated when entering the block
    > so it gets reset. not sure. but i have used scalar .. many times in
    > plain conditionals.
    ".." does an implicit compare with $. when it sees a literal. (It
    would be nicer if it only did that for numeric literals.) The solution
    is to use a non-literal expression. "do { 1}" and "do { 0}" work
    nicely for true and false. So:

    my @oldpresidents = grep { do { 1} .. $_ eq 'Bush' } @presidents;

    Anno

    Anno Siegel Guest

  14. #13

    Default Re: copy list until...

    Anno Siegel wrote:
    > my @oldpresidents = grep { do { 1} .. $_ eq 'Bush' } @presidents;
    Not good. Let me give an easier to test example:

    rint grep { do { 1 } .. $_ eq 'N' } 'A'..'Z';

    This is supposed to print letter 'A' .. 'N', no? Yet it doesn't stop at
    'N':

    ABCDEFGHIJKLMNOPQRSTUVWXYZ

    All of them. The problem is off course that when it finished at the end,
    it'll start testing again upfront, and pass!

    You need an extra flag, I'm afraid there's no way around it.

    print grep { !$a++ .. $_ eq 'N' } 'A'..'Z';
    -->
    ABCDEFGHIJKLMN

    Maybe somebody can find a way using double use of .. to eliminate the
    variable, but I'm not sure.

    --
    Bart.
    Bart Lateur Guest

  15. #14

    Default Re: copy list until...

    Bart Lateur (bart.lateur@pandora.be) wrote on MMMDCLXX September MCMXCIII
    in <URL:news:slbkmvo0lm5ab6nvcdcqtib30fgmtbhmmg@4ax.c om>:
    () Anno Siegel wrote:
    ()
    () > my @oldpresidents = grep { do { 1} .. $_ eq 'Bush' } @presidents;
    ()
    () Not good. Let me give an easier to test example:
    ()
    () rint grep { do { 1 } .. $_ eq 'N' } 'A'..'Z';
    ()
    () This is supposed to print letter 'A' .. 'N', no? Yet it doesn't stop at
    () 'N':
    ()
    () ABCDEFGHIJKLMNOPQRSTUVWXYZ
    ()
    () All of them. The problem is off course that when it finished at the end,
    () it'll start testing again upfront, and pass!
    ()
    () You need an extra flag, I'm afraid there's no way around it.
    ()
    () print grep { !$a++ .. $_ eq 'N' } 'A'..'Z';


    Sure there is:

    print grep {!($_ eq "N" .. do {0})} "A" .. "Z"'

    ---> ABCDEFGHIJKLM


    Or:

    perl -wle 'print grep {!(/N/ ... /N/)} "A".. "Z"'



    Abigail
    --
    BEGIN {$^H {q} = sub {pop and pop and print pop}; $^H = 2**4.2**12}
    "Just "; "another "; "Perl "; "Hacker\n";
    Abigail Guest

  16. #15

    Default Re: copy list until...

    Bart Lateur <bart.lateur@pandora.be> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    >
    > > my @oldpresidents = grep { do { 1} .. $_ eq 'Bush' } @presidents;
    >
    > Not good. Let me give an easier to test example:
    >
    > rint grep { do { 1 } .. $_ eq 'N' } 'A'..'Z';
    >
    > This is supposed to print letter 'A' .. 'N', no? Yet it doesn't stop at
    > 'N':
    >
    > ABCDEFGHIJKLMNOPQRSTUVWXYZ
    >
    > All of them. The problem is off course that when it finished at the end,
    > it'll start testing again upfront, and pass!
    Ugh... you're right. When I tested I must have seen what I expected,
    not what was actually there.
    > You need an extra flag, I'm afraid there's no way around it.
    >
    > print grep { !$a++ .. $_ eq 'N' } 'A'..'Z';
    > -->
    > ABCDEFGHIJKLMN
    >
    > Maybe somebody can find a way using double use of .. to eliminate the
    > variable, but I'm not sure.
    Abigail has suggested elegant solutions, but they're not very readable.

    More straightforward:

    my @chars = 'A' .. 'Z';
    print grep { $_ eq $chars[ 0] .. $_ eq 'N' } @chars;

    But that requires the original list to be a named array, not to mention
    elegance. It also has problems when the original list elements are not
    unique, but so do Abigail's solutions.

    Anno

    Anno
    Anno Siegel 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