Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture Text

Status
Not open for further replies.

jriggs420

Programmer
Sep 30, 2005
116
US
Hello World!

I'm trying to capture the text that follows a set pattern. For example:
___DATA_____
User name=jriggs

In this simple scenario I would like the script to search the text file until it finds the above line, and then set. $uname='jriggs'...Of course $uname won't always be 'jriggs', any ideas? TIA-

Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
check out grep

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Yup, I think that I can use grep. It's not the prettiest, but it works (at least in the test setup). Check it out
Code:
open (DATA, "data")|| die;

@crap=(<DATA>);
@name=grep(/user=/ , @crap);

foreach $line (@name){$line=~s/user=//;}
print @name;            
close (DATA);

__DATA___
blah blah
yada yada
user=joseph
userid ping pong
But I don't get why I can't use
Code:
$name=grep(/user=/ , $crap[0]);
I know it' got something to do with dreferencing the reference, but seems like what I have should do it, no?

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
grep works on lists, it might work if you use parenthesis:

($name) = grep(/user=/, ($crap[0]));

but seems illogical to do it that way.
 
Code:
my @names = map {/^user=(.+)/} <DATA>;
print "$_\n" for @names;


__DATA__
blah blah
yada yada
user=joseph
userid ping pong
user=fred
 
Code:
[b]#!/usr/bin/perl[/b]

while (<DATA>) {
  chomp;
  ($key, $value) = split(/=/);
  $uname = $value if $key eq "User name";
}

print "uname => $uname\n";

[blue]__DATA__
blah blah blah
User name=jriggs
blah blah[/blue]

Kind Regards
Duncan
 
Code:
#!/usr/bin/perl

/^User\s?name=(.+)/ and $uname = $1 while <DATA>;
print "\$uname eq $uname\n";

__DATA__
foo
bar
User name=jriggs
baz
TIMTOWTDI :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top