Here's something that shaves one line off, but it's not particularly pretty either. Also, it might not work in the context of your larger problem. I find the most annoying thing in this situation is that $foo and $bar are both new 'my' variables, and the 'my' declaration syntax is difficult to work with in other contexts that might create more elegance.<br>
<br><div style="margin-left: 40px;">richardmbp:~ rdice$ cat ./test.pl<br>#!/usr/bin/perl<br><br>use warnings;<br>use strict;<br><br>my ($foo, $bar);<br><br>($bar = $foo = "ABC-987-01") =~ s/(\w+-\d+)-\d+/$1/;<br>
<br>print "foo = $foo\n";<br>print "bar = $bar\n";<br><br>exit 0;<br>richardmbp:~ rdice$ perl ./test.pl<br>foo = ABC-987-01<br>bar = ABC-987<br></div><br>Alternatively, <br><br><div style="margin-left: 40px;">
richardmbp:~ rdice$ cat ./test2.pl<br>
#!/usr/bin/perl<br><br>
use warnings;<br>
use strict;<br><br>
my ($foo, $bar) = ("ABC-987-01");<br><br>
($bar = $foo) =~ s/(\w+-\d+)-\d+/$1/;<br><br>
print "foo = $foo\n";<br>
print "bar = $bar\n";<br><br>
exit 0;<br>
richardmbp:~ rdice$ perl ./test.pl<br>
foo = ABC-987-01<br>
bar = ABC-987<br></div>
<br>Cheers,<br> - Richard<br><br><div class="gmail_quote">On Tue, Jul 8, 2008 at 11:00 AM, Madison Kelly <<a href="mailto:linux@alteeve.com">linux@alteeve.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br>
<br>
I've got a simple problem I often come across, and I've got a way to make it work. However, I've always felt there must be a more ... elegant way of doing it.<br>
<br>
For example, let's say I want to copy a variable and strip a bit off the end;<br>
<br>
my $foo="ABC-987-01";<br>
my $bar=$foo;<br>
$bar=~s/(\w+-\d+)-\d+/$1/;<br>
# $bar now 'ABC-987'.<br>
<br>
That's three lines. Is there a way to do this in one line? Specifically, is there a way to assign '$1' to a new variable in one go?<br>
<br>
Thanks!<br>
<br>
Madi<br>
_______________________________________________<br>
toronto-pm mailing list<br>
<a href="mailto:toronto-pm@pm.org" target="_blank">toronto-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/toronto-pm" target="_blank">http://mail.pm.org/mailman/listinfo/toronto-pm</a><br>
</blockquote></div><br>