new here
am 06.05.2011 20:13:38 von wolken.flug
Hi all,
I´m new here and my English is not the best. However, I would like to =
get some help for my perl-problems.
I want to write a script which is finding data´s like "0.1" or "46.7" =
in a string and declare them as variables, which are to be changed and wrot=
e back.
The string is looking like: "test [0.4\9.0]"
My intention was to get data out of the []-brackets and set them to variabl=
es which can be changed.
the script (partially) I thought Ihave to use for this:Â
while (<$in> )
   {
   if ($in =3D~ /(-?\d+\.\d)\(-?\d+\.\d)/)
      {
      $leaf1 =3D $1;
      $leaf2 =3D $2;
   print "$leaf1\n";
   }
   }
isn´t working. There is nothing printed out were I expected to see "0.=
4".
Can someone tell me, where is the fault, please? And for the case, there ar=
e 80 data´s in the line, is there a shorter style to find & change the=
m?
Thanks for your help
regards Claudia
___________________________________________________________
Schon gehört? WEB.DE hat einen genialen Phishing-Filter in die
Toolbar eingebaut! http://produkte.web.de/go/toolbar
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: new here
am 06.05.2011 20:21:23 von Shawn H Corey
On 11-05-06 02:13 PM, wolken.flug@web.de wrote:
> if ($in =~ /(-?\d+\.\d)\(-?\d+\.\d)/)
Try:
if( $in =~ /(\d+\.\d+)\\(\d+\.\d+)/ )
You need two backslashes to match one.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: new here
am 06.05.2011 20:30:39 von Thomas Lingmann
Hi,
try something like
my @str =3D (
"test [0.4\\9.0]"
);
foreach (@str) {
if ( ($leaf1, $leaf2) =3D $_ =3D~ m{ (\d+\.\d)\\(\d+\.\d) }x ) {
print $leaf1, "\n";
print $leaf2, "\n";
}
}
-- Thomas
* wolken.flug@web.de [06.05.2011 20:16]:
> Hi all,
>=20
> I´m new here and my English is not the best. However, I would like t=
o get some help for my perl-problems.
>=20
> I want to write a script which is finding data´s like "0.1" or "46.7=
" in a string and declare them as variables, which are to be changed and wr=
ote back.
>=20
> The string is looking like: "test [0.4\9.0]"
>=20
> My intention was to get data out of the []-brackets and set them to varia=
bles which can be changed.
>=20
> the script (partially) I thought Ihave to use for this:Â
>=20
> while (<$in> )
> Â Â Â {
>=20
> Â Â Â if ($in =3D~ /(-?\d+\.\d)\(-?\d+\.\d)/)
> Â Â Â Â Â Â {
> Â Â Â Â Â Â $leaf1 =3D $1;
> Â Â Â Â Â Â $leaf2 =3D $2;
>=20
> Â Â Â print "$leaf1\n";
> Â Â Â }
> Â Â Â }
>=20
> isn´t working. There is nothing printed out were I expected to see "=
0.4".
>=20
> Can someone tell me, where is the fault, please? And for the case, there =
are 80 data´s in the line, is there a shorter style to find & change t=
hem?
>=20
> Thanks for your help
> regards Claudia
> ___________________________________________________________
> Schon gehört? WEB.DE hat einen genialen Phishing-Filter in die
> Toolbar eingebaut! http://produkte.web.de/go/toolbar
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: new here
am 06.05.2011 21:04:44 von Uri Guttman
>>>>> "TL" == Thomas Lingmann writes:
TL> Hi,
TL> try something like
TL> my @str = (
TL> "test [0.4\\9.0]"
TL> );
why the array for the data? if you had multiple tests, i can see that
but you only show one of them
TL> foreach (@str) {
even in simple examples it is better to use named vars in foreach
loops. it documents the code better and it is a good example for
production use.
TL> if ( ($leaf1, $leaf2) = $_ =~ m{ (\d+\.\d)\\(\d+\.\d) }x ) {
since you did use $_ as the loop var, you don't need it in the
expression as regexes will use it by default. you did use m{}x but
neither the m{} nor /x was needed. you have no / in the regex so // will
do fine. and you just put white space at the outside of the regex which
doesn't help much so the /x is not needed. if you spread out the regex
on multiple lines with comments, then /x is needed and useful.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: new here
am 07.05.2011 00:02:48 von Sandip Bhattacharya
On Fri, May 6, 2011 at 11:43 PM, wrote:
> Can someone tell me, where is the fault, please? And for the case, there =
are 80 data=B4s in the line, is there a shorter style to find & change them=
?
>
You showed a slash between the numbers but mentioned earlier that the
numbers can be in any format in the string. In any case, the code
below should work for that too. :)
A problem with your regex is that it will only match numbers with a
decimal point. Unless your input always has a decimal point, you need
to take care of whole numbers too.
Try this:
while( <$in> ) {
my @matches =3D m/(\d+\.\d+|\d+)/g; # This looks for decimal numbers
and then whole numbers
# II am matching on the default $_ up there. If you give an array
on the left side, you get all your matches in it
# if no matches the array will be empty
#for your example
print "$matches[0]\n" if @matches;
# if you want to print all the numbers, comma separated
print join(",", @matches), "\n" if @matches;
}
- Sandip
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/