#perl - Sun 25 Feb 2007 between 05:34 and 05:42

punterNice perl problem: How (in 1 line) would you remove every second element from an array?
kestasif I have some text, say 'ABC ABC DEF' , is it possible to build a regex that will take only the last ABC DEF. Like /ABC.*?DEF/ but going from the right instead of the left
punterkestas: yes
kestas: i think, do sthng like this:
f00li5heval: @foo = 1..10; [ map { $i ++ % 2 } @foo ]
buubotf00li5h: [0,1,0,1,0,1,0,1,0,1]
f00li5heval: @foo = 1..10; [ grep { $i ++ % 2 } @foo ]
buubotf00li5h: [2,4,6,8,10]
punterkestas: if you add ".*" at the beginning of your regexp...
kestas: so /.*ABC.*?DEF/ ... wouldn't that do it? (just a thought)
kestaspunter: ah but the pattern could be like ABC ABC DEF ABC ABC DEF
f00li5hkestas: what do you actually want?
punterkestas: see my answer
kestas: i think it's correct
kestaspunter: it's also across multiple lines
unopeval: $_="ABC ABC DEF ABC ABC DEF"; [/.*(ABC DEF)$/]
buubotunop: ['ABC DEF']
punterkestas: then add the 's' modifier at the end
kestas: /.*blabla/s
kestashmm interesting, thanks
punterAny takers for my interesting problem? How would you (in one line) take only the odd elements (or the even elements) from an array
f00li5hpunter: did that already
@foo = 1..10; [ grep { $i ++ % 2 } @foo ]
punterf00li5h: a... i'm sorry, i'll check
unopeval: [grep $_%2, 0..10]
buubotunop: [1,3,5,7,9]
puntervery nice, thanks
f00li5hunop: every second element, not every even element
punterbut you would need to write "my $i" on a separate line to conform with strict, right?
that's okay
heck no
unopf00li5h, errm, i was reading his last question -- take odd elements from an array
pasteling"zen" at pasted "Not reading in files well." (39 lines, 936B) at http://sial.org/pbot/23164
f00li5hpunter: if you my $i, it will not be global, and so it will fail
punter: (unless you do it outside the grep)

Page: 2 9 16 23 30 37 44 51 58 65 72 79 86 93 100