On 2011-12-04 18:12, Shlomi Fish wrote:
Chris wrote:

    my $cell = substr($market,0,index($market,"_"));

  print "$_ <", substr( $_, 0, index $_, "_" ), ">\n"
    for qw/ foo1 foo2_bar foo3_bar_baz /;

foo1 <foo>
foo2_bar <foo2>
foo3_bar_baz <foo3>

This can be more idiomatically (and more briefly) done using:

      (my $cell = $market) =~ s/_.*//s;

  print("$_ <"), (local $_ = $_) =~ s/_.*//s, print("$_>\n")
    for qw/ foo1 foo2_bar foo3_bar_baz /;

foo1 <foo1>
foo2_bar <foo2>
foo3_bar_baz <foo3>


Next alternative: my ($cell) = $market =~ s/([^_]*)/;

  print("$_ <"), local($_) = /([^_]*)/, print("$_>\n")
    for qw/ foo1 foo2_bar foo3_bar_baz /;

foo1 <foo1>
foo2_bar <foo2>
foo3_bar_baz <foo3>


But do realize that 'foo1' becomes 'foo' and not 'foo1', in the original code.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to