Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can this be combined into one statement?

Reply
Thread Tools

Can this be combined into one statement?

 
 
John Black
Guest
Posts: n/a
 
      10-28-2013
Simple question I think. I have a string $line that has some number of fields separated by
one or more spaces. The filename is the last field on the line and I want to grab it. There
must be a way to write these two lines as one line (skipping the intermediate @line_arr
step):

@line_arr = split(/\s+/, $line);
$file = $line_arr[-1];

but I've tried various syntaxes like:

$file = split(/\s+/, $line)[-1];

but I have not hit upon a working syntax. What is it? Thanks.

John Black
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-28-2013
John Black <(E-Mail Removed)> wrote:
>Simple question I think. I have a string $line that has some number of fields separated by
>one or more spaces. The filename is the last field on the line and I want to grab it. There
>must be a way to write these two lines as one line (skipping the intermediate @line_arr
>step):
>
> @line_arr = split(/\s+/, $line);
> $file = $line_arr[-1];
>
>but I've tried various syntaxes like:
>
> $file = split(/\s+/, $line)[-1];


You almost got it:
$file = (split(/\s+/, $line))[-1];

jue
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      10-28-2013
On 2013-10-28 23:03, John Black wrote:

> Simple question I think. I have a string $line that has some number of fields separated by
> one or more spaces. The filename is the last field on the line and I want to grab it. There
> must be a way to write these two lines as one line (skipping the intermediate @line_arr
> step):
>
> @line_arr = split(/\s+/, $line);
> $file = $line_arr[-1];
>
> but I've tried various syntaxes like:
>
> $file = split(/\s+/, $line)[-1];
>
> but I have not hit upon a working syntax. What is it? Thanks.


There are several ways to get there, examples:

$file = ( split " ", $line )[-1];

( $file ) = $line =~ /.*(\S+)/;


--
Ruud


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-29-2013
On 2013-10-28 23:03, John Black wrote:

> I have a string $line that has some number of fields separated by
> one or more spaces. The filename is the last field on the line
> [...] I've tried various syntaxes like:
>
> $file = split(/\s+/, $line)[-1];
>
> but I have not hit upon a working syntax. What is it? Thanks.


There are several ways to get there, examples:

$file = ( split " ", $line )[ -1 ];

( $file ) = $line =~ /.* (\S+) /x;


Also see Text::CSV_XS.

--
Ruud


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-29-2013
On 2013-10-29 00:56, Eli the Bearded wrote:
> In comp.lang.perl.misc, Dr.Ruud <(E-Mail Removed)> wrote:


>> ( $file ) = $line =~ /.*(\S+)/;

>
> $ perl -wle '$line = "a\tb c\td e\tfilename";
> ($file) = $line =~ /.*(\S+)/;
> print $file'
> e
>
> Did you actually try your examples?


Yeah, but only badly.

($file) = $line =~ /.*\s(\S+)/;

--
Ruud

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-29-2013
On 2013-10-29 01:01, Dr.Ruud wrote:
> On 2013-10-28 23:03, John Black wrote:


> ( $file ) = $line =~ /.* (\S+) /x;


Correction:

( $file ) = $line =~ /.*\s (\S+) /x;

--
Ruud

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-29-2013
On 2013-10-29 00:56, Eli the Bearded wrote:
> In comp.lang.perl.misc, Dr.Ruud <(E-Mail Removed)> wrote:


>> $file = ( split " ", $line )[-1];

>
> $ perl -wle '$line = "a\tb c\td e\tfilename";
> $file = ( split " ", $line )[-1];
> print $file'
> filename


That works as meant, see "perldoc -f split" about the specialness of a
single space as the first parameter of split.


But to take the original post literally ("spaces"), it should capture
"e\tfilename", so then the split should be done with / +/.

--
Ruud

 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      10-29-2013
John Black <(E-Mail Removed)> writes:
> Simple question I think. I have a string $line that has some number of fields separated by
> one or more spaces. The filename is the last field on the line and I want to grab it. There
> must be a way to write these two lines as one line (skipping the intermediate @line_arr
> step):
>
> @line_arr = split(/\s+/, $line);
> $file = $line_arr[-1];
>
> but I've tried various syntaxes like:
>
> $file = split(/\s+/, $line)[-1];
>
> but I have not hit upon a working syntax.


Grab a sequence of non-whitespace characters anchored at the end of the
string?

$line =~ /(\S+)$/
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      10-29-2013
John Black <(E-Mail Removed)> writes:
> Simple question I think. I have a string $line that has some number of fields separated by
> one or more spaces. The filename is the last field on the line and I want to grab it. There
> must be a way to write these two lines as one line (skipping the intermediate @line_arr
> step):
>
> @line_arr = split(/\s+/, $line);[*]
> $file = $line_arr[-1];
>
> but I've tried various syntaxes like:
>
> $file = split(/\s+/, $line)[-1]; [**]
>
> but I have not hit upon a working syntax.


Since nobody wrote this so far: The first split call ([*]) runs split in
list context, hence, it returns a list of strings created by it. But the
second ([**]) runs it in scalar context and then, it splits into @_ and
returns the number of fields found in the input.

$file = (split(' ', $line))[-1]

works as intended because the split is evaluated inside a list because
of the outer brackets.
 
Reply With Quote
 
John Black
Guest
Posts: n/a
 
      10-29-2013
In article <(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)
says...
>
> John Black <(E-Mail Removed)> writes:
> > Simple question I think. I have a string $line that has some number of fields separated by
> > one or more spaces. The filename is the last field on the line and I want to grab it. There
> > must be a way to write these two lines as one line (skipping the intermediate @line_arr
> > step):
> >
> > @line_arr = split(/\s+/, $line);
> > $file = $line_arr[-1];
> >
> > but I've tried various syntaxes like:
> >
> > $file = split(/\s+/, $line)[-1];
> >
> > but I have not hit upon a working syntax.

>
> Grab a sequence of non-whitespace characters anchored at the end of the
> string?
>
> $line =~ /(\S+)$/


ooo, nice. However, if there happens to be any whitespace between the last field and the end
of the line, I don't think this will work. But I think the split method would still be ok.
I don't know if there ever will be any spaces after the filename but probably better to use
code that would handle it.

John Black
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Who can explain this bug? mathog C Programming 57 06-11-2013 10:09 PM
Why does this incorrect CRTP static_cast compile? kfrank29.c@gmail.com C++ 2 04-25-2013 01:38 PM
This looks like a Perl bug George Mpouras Perl Misc 18 04-21-2013 11:56 PM
Really throwing this out there - does anyone have a copy of my oldDancer web browser? steven.miale@gmail.com Python 1 04-10-2013 03:32 PM
Combined AFTER and WHEN statement JB VHDL 1 07-07-2011 09:55 AM



Advertisments