There has got to be an easier way in perl to get a certain number of words from a line in a string than my code below. Could anyone quickly help me out?
Thanks,
js.
$_="2003-12-20 22:13:58 11 12.37.2.215 407 TCP_DENIED 4294968543 93 GET http ping.180solutions.com http://ping.180solutions.com/ - N
ONE - -";
@F=split(/\s+/);
foreach $i (0 .. 10){
$front.="$F[$i] ";
}
foreach $i (11 .. 15){
$back.="$F[$i] ";
}
print "\nFRONT=$front";
print "\nBACK=$back";
20040615 Edit by [Corion]: Changed title from 'subword'
array-slices are simpler and easier to read:
$front = join (' ', @F[0..10]);
$back = join (' ', @F[11..15]);
hth
regards,
tomte
An intellectual is someone whose mind watches itself.
-- Albert Camus
Bless you tomte!
Thanks,
js1.
my ($front, $back) = /((?:\S+\s+){11})((?:\S+\s+){5})/;
ought to work. I don't have perl5 where I am to test it.
$_=$l="how now brown cow hay is for horses oats are for goats mary little lamb";
#maybe a little inefficient, but no temp variables, all one line
($f,$b)=(join('',(split)[0..10]), join('',(split)[11..15]));
#is there a better way to compose functions? golfers?
($f,$b)=map {join('',(split(/\s+/,$l))[@$_])}([0..10],[11..15]);
Update: Changed the 2nd snippet above so it doesn't depend on an obscure bug in 5.8.0. For the record you should use array references instead of list refs.
\(0..10)is the same as
[0..10]Update: It appears that they are the same for this particular case, which is (IMO) a Bad Idea, since the \() construct is already a not-uncommon source of confusion. Why was it considered important to make \(something) behave like [something] iff something is a range?
$ref=\(1..10); @a[@$ref];...is an eleven item array slide of @a, which happens to be equivalent to @a[0..10]. Try it, you might like it ;)
It's not a feature I've used, myself, so I could certainly be misunderstanding it. If I am, I would genuinely appreciate someone making it clear to me.
It took me a minute to decode "array slide" as "array slice". I thought you were talking about a feature I'd never heard of!
Update: from perlref:
Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references!
greg@sparky:~/test$ cat ./list_ref #!/usr/bin/perl -w @a=qw/zero one two three four five/; $ref=\(1..3); @b=@a[@$ref]; print "\n \@a=@a\n \$ref=$ref\n \@b=@b\n\n"; greg@sparky:~/test$ ./list_ref @a=zero one two three four five $ref=ARRAY(0x813dcc0) @b=one two three greg@sparky:~/test$ perl -v This is perl, v5.8.0 built for i486-linux Copyright 1987-2002, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit....And sorry for the typo. This thread (among others) has finally inspired me to create my first perlmonks .sig.
greg@sparky:~/test$ cat enum
#!/usr/bin/perl -w
use Data::Dumper;
$ref=\(0..3);
$enum_list=\(4,5,6);
print "\n".Dumper($ref)."\n";
print "\n".Dumper($enum_list)."\n";
greg@sparky:~/test$ ./enum
$VAR1 = [
0,
1,
2,
3
];
$VAR1 = \6;
$x = \((0..3));?
As a general rule, I would recommend use of [0..10] instead of the capricious \(0..10), since the former is always a way to get an array ref (and is a char shorter), while the latter is usually a way to make a list of references.
perl -e '$x = \((0..3)); print @$x' 0123
perlmonks.org content © perlmonks.org and halley, js1, Roy Johnson, sleepingsquirrel, Tomte
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03